目前我有一个这样的数组:
var list = new Array([
{id: 0, content: "zero", group: 1},
{id: 1, content: "one", group: 2},
{id: 2, content: "two", group: 1},
{id: 3, content: "three", group: 1},
{id: 4, content: "four", group: 3},
]);
如何使用javascript或Jquery删除group == 1的条目?
答案 0 :(得分:1)
按以下方式更改数组:
var list = [
{id: 0, content: "zero", group: 1},
{id: 1, content: "one", group: 2},
{id: 2, content: "two", group: 1},
{id: 3, content: "three", group: 1},
{id: 4, content: "four", group: 3},
];
否则您将最终成为包含另一个数组的数组。之后,您可以通过以下方式过滤数组:
var filtered = list.filter(function(item) {
return item.group !== 1
});
console.log(filtered);
答案 1 :(得分:0)
您不需要使用new Array
- 您的代码会创建一个包含一个元素的数组,该元素本身就是一个包含五个元素的数组。您应该只使用数组文字:
var list = [
{id: 0, content: "zero", group: 1},
{id: 1, content: "one", group: 2},
{id: 2, content: "two", group: 1},
{id: 3, content: "three", group: 1},
{id: 4, content: "four", group: 3},
]
完成后,您可以使用Array.prototype.filter
:
var filtered = list.filter(function(item) {
return item.group !== 1;
})
如果您的意思是构建2D阵列,您仍然可以使用过滤器:
var filtered = [list[0].filter(function(item) {
return item.group !== 1;
})]
答案 2 :(得分:0)
在javascript中:
list =list.filter(function(item){
return item.group!=1
})