1
 let a = [1,2,40,60]
let b = [50, 70, 80]

Suppose I want to move 40 from a to array b and delete it from a so I get

a = [1,2,60]
b=[50, 70, 80, 40]

Please help. Any suggestion is appreciated

  • 1
    Does this answer your question? [How can I remove a specific item from an array?](https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array) – luk2302 Sep 19 '22 at 11:25
  • @luk2302 This one complements your answer [How to append something to an array?](https://stackoverflow.com/q/351409/9681386) – Javito Sep 19 '22 at 11:26

1 Answers1

1

You can do a simple splice to move the value:

b.push(a.splice(2, 1)[0])

This grabs the element you want from a, adds it to b, and removes it from a all at the same time.

Edit: As @malarres pointed out below, you can also concat the returned array:

b.concat(a.splice(2, 1))
Dan Mullin
  • 4,285
  • 2
  • 18
  • 34