-
catspeake
对于初学者,不返回新数组。如果要返回新数组,请改用。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"}]希望这有帮助!
-
沧海一幻觉
试试这个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);
-
慕运维8079593
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)
-
ibeautiful
你可以这样做,最简单的方法来做到这一点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)