gpt4 book ai didi

node.js - 从 javascript 对象数组中删除一些属性

转载 作者:太空宇宙 更新时间:2023-11-04 03:29:11 26 4
gpt4 key购买 nike

我想从 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。

最佳答案

您可以使用下划线来实现此目的。它更简单且更具可读性

obj_array.map(obj => _.omit(obj,["DATA_BB_TYP", "DATA_MAC"]))

关于node.js - 从 javascript 对象数组中删除一些属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40445193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com