0

I have array of string smth like this:

myarray = ["first word","second word","smth more",]

I need to delete each second word from array.

result:

myarray = ["first","second","smth",]
RosaC
  • 1
  • 1
  • 2
    Welcome to Stackoverflow! What have you tried so far to solve this? Where did you get stuck? – fjc Apr 22 '20 at 11:54
  • [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – fjc Apr 22 '20 at 11:54
  • Does this answer your question? [Loop through an array in JavaScript](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – caramba Apr 22 '20 at 12:13

1 Answers1

1

You could use Array.prototype.map()

var yourArray = ["first word","second word","smth more"];

var yourNewArray = yourArray.map( item => { 
   return item.split(' ')[0];
});


console.log(yourNewArray);

or about the same with Array.prototype.forEach()

var yourArray = ["first word","second word","smth more"];
var yourNewArray = [];

yourArray.forEach( item => {
    yourNewArray.push(item.split(' ')[0])
});

console.log(yourNewArray);
caramba
  • 21,963
  • 19
  • 86
  • 127