javascript - 在数组中查找对象?

标签 javascript arrays reactjs object

假设我有一个像这样的对象数组(伪代码):

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/

相关文章:

python - 交织两个numpy数组

reactjs - 如何使用 Jest 和 react 测试库在 react 中测试路线

javascript - 使用深度比较或 json.stringify 比较两个对象?

javascript - 如何在页面加载时使用 mysql 查询结果设置单选按钮值后触发 javascript 函数

javascript - 在 getElementByID 中获取动态 ID(来自 AngularJS)

php - 如何在 preg_replace 中使用数组和正则表达式作为模式?

javascript - 如果管道中的文件是 javascript,Gulp 任务应用 uglify

javascript - 如何将用逗号分隔的纯文本更改为 Node 中的数组?

reactjs - React 中的 PropType

javascript - 如何将时间格式化为 dayjs 对象?