我有一个数组、过滤器和关键字。 我想使用过滤器和关键字在该数组中搜索,以及对象的结果数组。就像第一个数组一样。
var data = [
{email: "[email protected]",nama:"User A", Level:"Super Admin"},
{email: "[email protected]",nama:"User B", Level:"Super Admin"},
{email: "[email protected]",nama:"User C", Level:"Standart"},
{email: "[email protected]",nama:"User D", Level:"Standart"},
{email: "[email protected]",nama:"User E", Level:"Admin"},
{email: "[email protected]",nama:"User F", Level:"Standart"}
];
var filter = "Level";
var keyword = "Standart";
//--------Search
console.log(data);
Array.prototype.filter
函数,该函数接受回调并进行相应的过滤。根据文档:
方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。filter()
回调是提供的函数,它带有三个参数。来自文档:
callback
函数是一个谓词,用于测试数组的每个元素。返回 true 以保留元素,否则返回 false,采用三个参数:
element
数组中当前正在处理的元素。
index
数组中当前正在处理的元素的索引。
array
调用了数组过滤器。
我们可以使用
element
来检查当前元素并测试是否应该过滤它,如下所示:
var data = [
{email: "[email protected]",nama:"User A", Level:"Super Admin"},
{email: "[email protected]",nama:"User B", Level:"Super Admin"},
{email: "[email protected]",nama:"User C", Level:"Standart"},
{email: "[email protected]",nama:"User D", Level:"Standart"},
{email: "[email protected]",nama:"User E", Level:"Admin"},
{email: "[email protected]",nama:"User F", Level:"Standart"}
];
var filter = "Level";
var keyword = "Standart";
var filteredData = data.filter(function(obj) {
return obj[filter] === keyword;
});
console.log(filteredData);
在这里,我们使用回调(测试)来检查
obj
中指定的当前元素 (filter
) 的属性是否严格等于 keyword
。如果通过,则保留,因此所有具有属性 Level
和值 Standart
的对象都将被保留。您还可以使用 ES6 箭头函数: 缩短此长度
var filteredData = data.filter((obj) => obj[filter] === keyword);
这只是上面的简写。它实际上是相同的,根据当前元素的
Level
属性是否严格等于 keyword
返回 true 或 false。
array.prototype.filter()
函数通过自定义函数过滤数组:
var data = [
{email: "[email protected]",nama:"User A", Level:"Super Admin"},
{email: "[email protected]",nama:"User B", Level:"Super Admin"},
{email: "[email protected]",nama:"User C", Level:"Standart"},
{email: "[email protected]",nama:"User D", Level:"Standart"},
{email: "[email protected]",nama:"User E", Level:"Admin"},
{email: "[email protected]",nama:"User F", Level:"Standart"}
];
var filter = "Level";
var keyword = "Standart";
//--------Search
var filtered_data = data.filter(function(item) {
return item[filter] == keyword
})
console.log(filtered_data);
您可以使用
filter()
函数根据如下条件过滤掉元素:
> var data = [
{email: "[email protected]",nama:"User A", Level:"Super Admin"},
{email: "[email protected]",nama:"User B", Level:"Super Admin"},
{email: "[email protected]",nama:"User C", Level:"Standart"},
{email: "[email protected]",nama:"User D", Level:"Standart"},
{email: "[email protected]",nama:"User E", Level:"Admin"},
{email: "[email protected]",nama:"User F", Level:"Standart"}
];
> var newData=data.filter(function(x) { return x.Level == 'Standart'})
> console.log(JSON.stringify(newData))
[{"email":"[email protected]","nama":"User C","Level":"Standart"},{"email":"[email protected]","nama":"User D","Level":"Standart"},{"email":"[email protected]","nama":"User F","Level":"Standart"}]
你可以在这里使用数组过滤方法来完成你的目标。
var data = [{
email: "[email protected]",
nama: "User A",
Level: "Super Admin"
}, {
email: "[email protected]",
nama: "User B",
Level: "Super Admin"
}, {
email: "[email protected]",
nama: "User C",
Level: "Standart"
}, {
email: "[email protected]",
nama: "User D",
Level: "Standart"
}, {
email: "[email protected]",
nama: "User E",
Level: "Admin"
}, {
email: "[email protected]",
nama: "User F",
Level: "Standart"
}];
var filter = "Level";
var keyword = "Admin";
var filteredArray = data.filter((item) => item[filter] === keyword);
console.log(filteredArray);