这是数组变量
let packageitems=
[
{
"packageid":1,
"items":[
{
"itemid":"1",
"name":"abc"
},
{
"itemid":"2",
"name":"cdr"
}
]
},
{
"packageid":2,
"items":[
{
"itemid":"1",
"name":"abc"
},
{
"itemid":"2",
"name":"xyz"
}
]
}
]
在angular typescript中我有一个功能:
updatePackageitem(newitem){
let objIndex = this.packageitems.findIndex(obj => obj.packageid==newitem.packageid);
Object.assign(this.packageitems[objIndex] , newitem);
}
newitem
json类似于items数组不同
当我使用Object.assign分配时newitem
数组元素获得this.packageitems
的所有其余json的副本,即使在获得objIndex
权限之后
newitem =
{
"packageid":2,
"items":[
{
"itemid":"1",
"name":"def"
},
{
"itemid":"2",
"name":"pqr"
}
]
}