我想从javascript对象数组中删除一些属性。这是对象数组。
obj_array = [{
"DATA_ID": 1,
"DATA_NAME": "Jim",
"DATA_BB_TYP": 2,
"DATA_MAC": "5474",
},
{
"DATA_ID": 3,
"DATA_NAME": "Fro",
"DATA_BB_TYP": 33,
"DATA_MAC": "8e30",
},
{
"DATA_ID": 2,
"DATA_NAME": "Jimb",
"DATA_BB_TYP": 2,
"DATA_MAC": "45e8",
},
{
"DATA_ID": 4,
"DATA_NAME": "Kht1",
"DATA_BB_TYP": 35,
"DATA_MAC": "58d0",
},
{
"DATA_ID": 6,
"DATA_NAME": "Sens",
"DATA_BB_TYP": 34,
"DATA_MAC": "d004",
}
]
我有这个字符串数组,它指定要删除的属性。
var str_array_criteria = ["DATA_BB_TYP", "DATA_MAC"];
删除后,对象数组将如下所示;
obj_array_removed = [{
"DATA_ID": 1,
"DATA_NAME": "Jim",
},
{
"DATA_ID": 3,
"DATA_NAME": "Fro",
},
{
"DATA_ID": 2,
"DATA_NAME": "Jimb",
},
{
"DATA_ID": 4,
"DATA_NAME": "Kht1",
},
{
"DATA_ID": 6,
"DATA_NAME": "Sens",
}
]
我正在使用node.js v6。
答案 0 :(得分:2)
你走了,
var obj_array = [{
"DATA_ID": 1,
"DATA_NAME": "Jim",
"DATA_BB_TYP": 2,
"DATA_MAC": "5474",
},
{
"DATA_ID": 3,
"DATA_NAME": "Fro",
"DATA_BB_TYP": 33,
"DATA_MAC": "8e30",
},
{
"DATA_ID": 2,
"DATA_NAME": "Jimb",
"DATA_BB_TYP": 2,
"DATA_MAC": "45e8",
},
{
"DATA_ID": 4,
"DATA_NAME": "Kht1",
"DATA_BB_TYP": 35,
"DATA_MAC": "58d0",
},
{
"DATA_ID": 6,
"DATA_NAME": "Sens",
"DATA_BB_TYP": 34,
"DATA_MAC": "d004",
}
];
var str_array_criteria = ["DATA_BB_TYP", "DATA_MAC"];
var new_obj_array = obj_array.map(function(obj) {
str_array_criteria.forEach(function(prop) {
delete obj[prop];
});
return obj;
});
console.log(new_obj_array);

答案 1 :(得分:1)
您可以使用下划线实现此目的。它更加简单易读
obj_array.map(obj => _.omit(obj,["DATA_BB_TYP", "DATA_MAC"]))