es6 复制对象

var pp = {'name': '1','work': 'teacher'}
var kk = [1,2]
var tt = []
for(let index = 0; index<kk.length;index++) {
    tt.push({...pp})
}
for(let index = 0; index<kk.length;index++) {
    tt[index].age = kk[index]
}
console.log(tt)


  1. tt(2) [{…}, {…}]
    1. 0:{name: "1", work: "teacher", age: 1}
    2. 1:{name: "1", work: "teacher", age: 2}
    3. length:2
    4. __proto__:Array(0)
 

猜你喜欢

转载自www.cnblogs.com/xuyan1/p/9107723.html