编辑:包含更多详细信息
嗨,我在 jQuery 中有一个对象数组,如下所示,
我的问题是如何通过columnheader
作为参数从该对象数组中删除记录。我知道有这个
var result = $.grep(records, function(e){ return e.columnheader == currentheader; });
但是grep
我只是用来根据我传入的currentheader
检查是否有匹配的数据。如果我想删除怎么办?
当我在该对象数组中循环时,我想动态删除一条记录,让我这样做。 data
包含图像中显示的所有对象数组。
$.each(data, function(key,value) {
// Let's say I want the code here to delete a record in the current object array that I'm looping into.
});
谢谢
最佳答案
您可以使用filter()
The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.
arr = arr.filter(function(e) {
return e.columnheader !== currentheader;
});
演示
var arr = [{
name: 'John Skeet',
rank: 1
}, {
name: 'T.J.Crowder',
rank: 10
}];
console.log(arr);
arr = arr.filter(function(e) {
return e.rank !== 10
});
console.log(arr);
更新
I want the code here to delete a record in the current object array that I'm looping into
更改数组中对象的属性。
var arr = [{
name: 'John Skeet',
rank: 1
}, {
name: 'T.J.Crowder',
rank: 10
}];
$.each(arr, function(index, obj) {
if (obj.rank === 10) {
arr[index].rank = 9;
}
});
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
关于javascript - 删除对象数组中的记录 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32306596/