4 回答
TA贡献1111条经验 获得超0个赞
对于初学者,不返回新数组。如果要返回新数组,请改用。Array.prototype.forEachArray.prototype.map
我之所以这么说,只是因为您将其分配给测试,这将返回未定义。
有了这个,他们应该工作
data.forEach(item => {
for(const key in item) {
if(Array.isArray(item[key])) {
delete item[key]
}
}
})
以下是输入和输出的代码:
输入:
const data = [
{
"ID": 123,
"ItemName": "Item1",
"CommentList": [
{
"CommentID": "ABC",
"CommentText": "Test entry"
}
],
"ExtraList": [
"A123B"
]
},
{
"ID": 124,
"ItemName": "Item1",
"CommentList": [
{
"CommentID": "ABC",
"CommentText": "Test entry"
}
],
"ExtraList": [
"A123B"
]
}
]
输出:
[
{ID: 123, ItemName: "Item1"},
{ID: 124, ItemName: "Item1"}
]
希望这有帮助!
TA贡献1824条经验 获得超5个赞
试试这个
const a = {
"ID": 123,
"ItemName": "Item1",
"CommentList": [
{
"CommentID": "ABC",
"CommentText": "Test entry"
}
],
"ExtraList": [
"A123B"
]
}
const obj = {};
Object.keys(a)
.filter(key => !(a[key] instanceof Array))
.forEach(key => obj[key] = a[key]);
console.log(obj);
TA贡献1876条经验 获得超5个赞
arr是一个对象,你可以得到一个键数组,然后过滤它。Object.keys(obj)
let obj = {
"ID": 123,
"ItemName": "Item1",
"CommentList": [{
"CommentID": "ABC",
"CommentText": "Test entry"
}],
"ExtraList": [
"A123B"
]
}
let test = Object.keys(obj).filter((key) => {
return !Array.isArray(obj[key])
})
console.log(test)
TA贡献1993条经验 获得超5个赞
你可以这样做,最简单的方法来做到这一点
const data = [
{
"ID": 123,
"ItemName": "Item1",
"CommentList": [
{
"CommentID": "ABC",
"CommentText": "Test entry"
}
],
"ExtraList": [
"A123B"
]
},
{
"ID": 124,
"ItemName": "Item2",
"CommentList": [
{
"CommentID": "CDE",
"CommentText": "Test entry 2"
}
],
"ExtraList": [
"A123BC"
]
}
]
let finalOutput=JSON.parse(JSON.stringify(data,['ID','ItemName']))
console.log(finalOutput)
添加回答
举报