我有一个数组对象
var customers = [{
"id": "1",
"name": "1",
"position": "1",
"office": "<button data-id=2 class='btn btn-danger'><i class='fa fa-trash fa-lg'></i> Delete record</button>",
"active": "1"
}, {
"id": "2",
"name": "2",
"position": "2",
"office": "<button data-id=2 class='btn btn-danger'><i class='fa fa-trash fa-lg'></i> Delete record</button>",
"active": 0
}];我需要的是做一个只有活跃客户的新数组,这个新数组看起来就像
var activeCustomers = [{
"id": "1",
"name": "1",
"position": "1",
"office": "1",
"active":"1"
}
}];因为你可能会看到只有一个活跃的客户?
发布于 2014-09-29 13:43:30
您可以在.filter上使用array.prototype (MDN参考)
var activeCustomers = customers.filter(function(customer) { return customer.active; });注意事项:您必须使用MDN填充来支持IE9下面的浏览器。
发布于 2014-09-29 13:45:59
您可以使用jQuery函数grep
var activeCustomers = $.grep(customers, function(c){return c.active;});请注意,这在所有浏览器中都是可行的;另一种方法是使用Array.filter,这是JavaScript (相对)的一个新添加,在一些旧的浏览器中会失败。
https://stackoverflow.com/questions/26101353
复制相似问题