Copying of an array of objects to another Array without object reference in javascript(Deep copy)

后端 未结 7 502
说谎
说谎 2020-12-01 05:05

I have a scenario where i need to copy the array of Objects(Main array) to another Temp array which should not have object reference basically if i make any modification to

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 05:52

    Use angular.copy. But not for the whole array (because it would pass array items by reference), but iterate it and use angular.copy on its members.

    var newArray = [];
    for (var i = 0, item; item = mainArray[i];) {
        newArray[i] = angular.copy(item);
        i++;
    }
    

提交回复
热议问题