javascript - 如何在javascript中删除数组对象项?

标签 javascript node.js

这是我的代码

var array = [{ id: 1,  name: 'test' }, { id: 2,  name: 'test2' }];

我需要像下面这样更改上面的数组

[{ name: 'test' }, { name: 'test2' }]

我尝试删除

array.forEach(function(arr, i) {
    delete array[i].id;
});
console.log(array);

输出为

[ { id: 1, name: 'test' },
  { id: 2,  name: 'test2'} ]

但它不会删除 id 项。如何删除数组对象项?

我在 Node v0.8中使用它。

最佳答案

id 属性被删除,如下所示:

for (var l in array[0]) {
    if (array[0].hasOwnProperty(l)) {
        console.log(array[0][l]);
    }
}

参见jsFiddle

node.js 输出的屏幕截图:

screenshot
(来源:nicon.nl)

关于javascript - 如何在javascript中删除数组对象项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17401657/

相关文章:

javascript - 更改 node_args 后 Pm2 重启 Node.js 进程

javascript - 使用 node.js 将值传递到代理页面

mysql - Nodejs/Express 从 Web 客户端保存选择的头像并直接保存到 MySQL 数据库

javascript - 'const' 在 ES6 中可用(使用 'esversion: 6' )

javascript - 使用 Google Maps API 将纬度和经度转换为街道地址

android - Mongoose 时间戳和纪元转换为 Date() 的问题

node.js - 在express api中返回时,mongoose中_id的类型错误

node.js - 如何将电子表格.values.batchUpdate 与 Google Cloud Functions 和 NodeJS 以及 'googleapis' 模块结合使用

javascript - 与 Redux react : error on propType property

javascript - 排除 iOS 运行 JavaScript