Deep copy the contents of an array of applications

When array operations, you may want multiple operations and reorganization of the array, it is best to use the time to push deep copy operation, or just push a reference address

Such as:

arr.push(Object.assign({}, item2))

 

example:

                           for(let i = 0; i < res.data.resultList.length; i++) {
                            let item1 = res.data.resultList[i]
                            console.log('item11111:', item1)
                            let flag = false
                            for(let j = 0; j < res.data.existList.length; j++) {
                                let item2 = res.data.existList[j]
                                if (item2.communityId === item1.communityId) {
                                    item2.communityName = item1.communityName
                                    item2.propertyName                                 arr.push (ITEM1)= Item1.propertyName
                                     // arr.push (item2) wrong! Avoid this 
                                    arr.push (Object.assign ({}, ITEM2)) // need deep copy operation, an address or just push REFERENCE 
                                    In Flag = to true 
                                } 
                                IF (In Flag) {
                                     BREAK 
                                } 
                            } 
                            IF (! In Flag) {
                                 / / Debugger

                            } 
                        } 
// following index assignment may be confused
// arr.forEach ((Item, index) => { // item.index = index +. 1 // })

 

Guess you like

Origin www.cnblogs.com/benbendu/p/11807374.html