gpt4 book ai didi

javascript - 根据属性从数组中删除对象的最佳方法?

转载 作者:行者123 更新时间:2023-11-28 12:42:14 25 4
gpt4 key购买 nike

根据属性从数组中删除对象的最佳方法是什么?

things = [{id:'23', color:'blue'},{id:'54', color:'red'},{id:'132', color:'green'} ]

我知道我需要删除的id'54'。从数组中删除该对象的最佳过程是什么?结果应为 things = [{id:'23', color: 'blue'},{id:'132', color:'green'}]

我可以运行一个循环并查看 id,如果匹配则将其删除,但我一直在寻找更好的方法。

谢谢!

最佳答案

使用过滤功能:

things = things.filter(function(val){return val.id!='54'});

关于javascript - 根据属性从数组中删除对象的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11390540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com