How To Push The Array Of Data To Another Array Through Javascript Without Loop
I have an Json array like data {'alphaNumeric':[]}. Here I just want to push the another array [mentioned below] of objects to this Data with out loop concept. data{'numeric':[{'id
Solution 1:
One solution may be using the concat
method. Which isn't really good as it creates a whole new array.
b.alphaNumeric = b.alphaNumeric.concat(a.numeric);
But there is a much better solution using push
. It accepts more than just one element, but unfortunately not as an array. This can be however achieved using its apply
method:
b.alphaNumeric.push.apply(b.alphaNumeric, a.numeric);
Also you can write your own method (I call it add
) which will do this action for you:
Array.prototype.add = function (array) {
this.push.apply(this, array);
returnthis;
};
b.alphaNumeric.add(a.numeric);
Solution 2:
Solution 3:
.push()
and .pop()
are for adding and removing single elements in an array. The return value from .concat() is what you're looking for:
var newArr = oldArr.concat(extraArr);
Post a Comment for "How To Push The Array Of Data To Another Array Through Javascript Without Loop"