I have two arrays that contain objects. arr1
and arr2
. If I unshift arr1
to contain the elements of arr2
, the elements are not appended individually into arr1
, instead the whole array ( arr2
) is appended at the beginning, for example:
arr1 = [{ele:1}, {ele:2}]
arr2 = [{ele:3}, {ele:4}]
I get: arr1 = [[{ele:3}, {ele:4}], {ele:1}, {ele:2}]
Notice the whole array inside instead of its objects.
I want: arr1 = [{ele:3}, {ele:4}, {ele:1}, {ele:2}]
My arrays are ko.observableArray(), maybe that makes a difference?
你去吧:
[].unshift.apply(arr1, arr2);
maybe this might be bit simpler
let arr1 = [{ ele: 1 }, { ele: 2 }];
let arr2 = [{ ele: 3 }, { ele: 4 }];
for (const iterator of arr2) {
arr1.unshift(iterator);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.