开发者

Deleting specific keys from an array of object

开发者 https://www.devze.com 2023-03-21 03:50 出处:网络
var arrayObjects = [{key1:\'value\',key2:\'value2\'},{\'开发者_运维百科key1:\'value\',key2:\'value2}]
var arrayObjects = [{key1:'value',key2:'value2'},{'开发者_运维百科key1:'value',key2:'value2}]

for(i = 0; i <arrayObjects.length; i++) {
    for (key in arrayObjects[i]) {
          delete key1; // Delete the key
          delete arrayObjects[i]['key1'] // Delete the Key's Value

    }

}

I get a error message 'cannot delete property of null'


You don't need delete key1;, just do:

for(i = 0; i <arrayObjects.length; i++) {
    delete arrayObjects[i]['key1'] // Delete the Key
}

If you only want to delete key1 from each array entry.

0

精彩评论

暂无评论...
验证码 换一张
取 消