我想从JSON对象数组中删除一个对象。 这是数组:
app1/CMakeLists.txt
例如,如何删除键为var standardRatingArray = [
{ "Q": "Meal",
"type": "stars"
},
{ "Q": "Drinks",
"type": "stars"
},
{ "Q": "Cleanliness",
"type": "stars"
}
];
的对象?
当然最好不要迭代数组。
提前致谢。
答案 0 :(得分:4)
您必须找到要删除的项的索引,因此您必须至少部分地迭代该数组。在ES6中,我会这样做:
const standardRatingArray = [
{ "Q": "Meal",
"type": "stars"
},
{ "Q": "Drinks",
"type": "stars"
},
{ "Q": "Cleanliness",
"type": "stars"
}
];
const index = standardRatingArray.findIndex(x => x.Q === "Drinks");
if (index !== undefined) standardRatingArray.splice(index, 1);
console.log("After removal:", standardRatingArray);