假设我有一个像这样的对象数组(伪代码):
const chosenBets = [{...}, {...}] // 2 items
我想从数组中删除一个特定的项目:
{id: 0, // is unique
label: 1,
odd: 1.33,
oddIndex: 0,
team_home: "Liverpool",
team_away: "Sheffield United",
matchCardIndex: 0,}
现在这个数组是:
const chosenBets = [{...}] // 1 items
我将如何实现这一目标?
最佳答案
你可以使用数组filter
const chosenBets = [{
id: 0, // is unique
label: 1,
odd: 1.33,
oddIndex: 0,
team_home: "Liverpool",
team_away: "Sheffield United",
matchCardIndex: 0
}, {
id: 1, // is unique
label: 1,
odd: 1.33,
oddIndex: 0,
team_home: "Liverpool",
team_away: "Sheffield United",
matchCardIndex: 0
}];
const filteredData = chosenBets.filter(item => item.id === 1);
console.log(filteredData);
关于javascript - 在数组中查找对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58147210/