从对象数组中删除特定键

时间:2011-07-19 15:06:56

标签: javascript

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

    }

}

我收到错误消息'cannot delete property of null'

1 个答案:

答案 0 :(得分:4)

您不需要delete key1;,只需执行:

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

如果您只想从每个数组条目中删除key1