可能对初学者来说这是一个非常明显的问题:
如果我拥有以下数组...
var arr =
[
{id: 1, item: "something", description: "something something"},
{id: 2, item: "something else", description: "something different"},
{id: 3, item: "something more", description: "more than something"}
]
...并希望通过调用ID(在这种情况下,通过点击具有相应ID的div)来删除其中的特定对象...
var thisItem = $(this).attr("id");
我能否不使用for循环来匹配arr[i]
和thisItem
?如果可以,怎么做呢?我的数组很大,运行for循环似乎太笨重了。
谢谢!
arr.splice(arr.findIndex(o => o.id === 2), 1);
- Tushar