[SOLVED] If given two arrays with integer data how to move one element to other array and delete that element from where it was or vice versa


This Content is from Stack Overflow. Question asked by Vikash Sharma

 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


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))

Answered by Dan Mullin, This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5.CC BY-SA 3.0.CC BY-SA 4.0.

people found this article helpful. What about you?