If I have an object defined as:
var myObj={};
Then, I update this object with:
myObj['fruit']=['apple', 'orange'];
Later, I would like to append "[banana, melon]" to myObj['fruit'], that’s update myObj to
['apple','orange','banana','melon']
what is the most elegant way to update ‘fruit’ attribute value of myObj in my case? That’s update array by appending a new array.
——–EDIT——-
I need a way to append array as one variable, not extract each element of the appended array and push. e.g. oldArray append with newArray = final array
JavaScript has a built in
Array.push()There are a few ways to approach appending an array. First up, use
apply()to call push with the array as individual arguments:Also, you could
concat()the arrays, however concat doesn’t modify the original array so if you have other references they might get lost: