-2

I would like to loop through and array and then get the last two letters from each string and create a new array with them?

my current array is

myArray = [Qe3,Ke4,Qe5,Je6]

I would like to end up with this

newArray = [e3,e4,e5,e6]

this is for www.chessonline.app

2
  • Can you please share what you've tried so far and explain what you're struggling with in particular in your code? Commented Jan 8, 2023 at 8:02
  • Does this answer your question? Slice each string-valued element of an array in Javascript Commented Jan 8, 2023 at 8:17

3 Answers 3

1

you need to loop over the array and cut first character !

 const myArray = ['Qe3','Ke4','Qe5','Je6']
 const newArr = myArray.map((el) => el.substring(1));
 
Sign up to request clarification or add additional context in comments.

Comments

0

Use slice


const newArray = myArray.map(str => str.slice(-2)); // Use map() to apply a function to each element of the array
// The function uses slice() to return the last two characters of each string

console.log(newArray); // ['e3', 'e4', 'e5', 'e6']


Comments

0

We can use a slice here:

var myArray = ["Qe3", "Ke4", "Qe5", "Je6"];
var output = myArray.map(x => x.slice(-2));
console.log(output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.