javascript - 删除对象数组中的记录 jQuery

标签 javascript jquery arrays object

编辑:包含更多详细信息

嗨,我在 jQuery 中有一个对象数组,如下所示,

enter image description here

我的问题是如何通过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/

相关文章:

javascript - jquery中如何默认折叠所有 Accordion ?

javascript - 移动浏览器和 100% x 100% 固定元素

javascript - 禁用移动设备上的滚动偏移

javascript - 如何在 summernote 中为 header 标签添加自定义样式

Java:添加两个数字字符数组

javascript - 在对象属性上使用 jQuery 方法

jquery - 阻止链接,确认然后转到位置jquery

javascript - 从 php 到没有 history.back() 的 html 页面 div id 的后退按钮

javascript - 显示具有多个对象的数组值

javascript - ES6 中的扩展运算符在旧版 JavaScript 中转换成什么?它比 array.concat 更 coSTLier 吗?