对象的javascript过滤器数组
我有一个对象数组,我想知道搜索它的最佳方法。给定以下示例,我如何搜索对象的javascript过滤器数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有一个对象数组,我想知道搜索它的最佳方法。给定以下示例,我如何搜索name=“Joe”和年龄{ 返回name.name==“Joe”&&name.ageObject.values(item.includes(searchVal)); } 让数据=[ {“名称”:“苹果”,“类型”:“水果”,“id”:123234}, {“名称”:“猫”,“类型”:“动物”,“id”:98989}, {“name”:“something”,“type”:“other”,“id”:6565}] log(“按名称过
name=“Joe”
和年龄<30
?jQuery有什么可以帮助的吗?或者我必须自己强制执行这个搜索吗
var names = new Array();
var object = { name : "Joe", age:20, email: "joe@hotmail.com"};
names.push(object);
object = { name : "Mike", age:50, email: "mike@hotmail.com"};
names.push(object);
object = { name : "Joe", age:45, email: "mike@hotmail.com"};
names.push(object);
您可以使用:
var found\u names=$.grep(名称,函数(v){
返回v.name==“Joe”&&v.age<30;
});
演示:您可以使用
[]轻松完成此操作。筛选方法:
var filterednames = names.filter(function(obj) {
return (obj.name === "Joe") && (obj.age < 30);
});
var filterednames=names.filter(函数(obj){
返回(对象名称==“Joe”)&&(对象年龄<30岁);
});
您需要为不支持[]的浏览器添加一个垫片。filter
方法:提供这样的代码。问题很快。如果您有两个对象数组,并且希望“对齐”这些对象数组,以便确保每个数组的对象的顺序与另一个数组的相同,该怎么办?如果您不知道数组中的任何对象包含哪些键和值怎么办。。。更不用说他们的顺序了
因此,您的[].filter
,[].map
等需要一个“通配符表达式”。如何获得通配符表达式
var jux = (function(){
'use strict';
function wildExp(obj){
var keysCrude = Object.keys(obj),
keysA = ('a["' + keysCrude.join('"], a["') + '"]').split(', '),
keysB = ('b["' + keysCrude.join('"], b["') + '"]').split(', '),
keys = [].concat(keysA, keysB)
.sort(function(a, b){ return a.substring(1, a.length) > b.substring(1, b.length); });
var exp = keys.join('').split(']b').join('] > b').split(']a').join('] || a');
return exp;
}
return {
sort: wildExp
};
})();
var sortKeys = {
k: 'v',
key: 'val',
n: 'p',
name: 'param'
};
var objArray = [
{
k: 'z',
key: 'g',
n: 'a',
name: 'b'
},
{
k: 'y',
key: 'h',
n: 'b',
name: 't'
},
{
k: 'x',
key: 'o',
n: 'a',
name: 'c'
}
];
var exp = jux.sort(sortKeys);
console.log('@juxSort Expression:', exp);
console.log('@juxSort:', objArray.sort(function(a, b){
return eval(exp);
}));
还可以在每个对象的迭代过程中使用此函数,为每个对象中的所有键创建更好的集合表达式,然后以这种方式过滤数组
这是API并置的一个小片段,我已经差不多完成了,它完成了,对象相等与豁免,对象单位和数组压缩。如果这些是您的项目需要或想要的东西,请评论,我会尽快让lib可以访问
希望这有帮助!快乐编码:)您可以利用函数从匹配元素的子集返回元素
var名称=[
{姓名:“乔”,年龄:20岁,电子邮件:joe@hotmail.com"},
{姓名:“迈克”,年龄:50岁,电子邮件:mike@hotmail.com"},
{姓名:“乔”,年龄:45岁,电子邮件:mike@hotmail.com"}
];
变量filteredNames=$(名称).filter(函数(idx){
返回名称[idx]。名称==“Joe”和名称[idx]。年龄<30;
});
$(filteredNames).each(函数(){
$('#output').append(this.name);
});代码>
var名称列表=[
{姓名:'x',年龄:20岁,电子邮件:'x@email.com'},
{姓名:'y',年龄:60岁,电子邮件:'y@email.com'},
{姓名:'Joe',年龄:22岁,电子邮件:'joe@email.com'},
{姓名:'Abc',年龄:40岁,电子邮件:'abc@email.com'}
];
var filteredValue=nameList.filter(函数(项){
return item.name==“Joe”&&item.age<30;
});
//以数组形式查看输出结果
log(JSON.stringify(filteredValue))代码>
var名称=[{
姓名:“乔”,
年龄:20,,
电子邮件:“joe@hotmail.com"
},
{
姓名:“迈克”,
年龄:50,,
电子邮件:“mike@hotmail.com"
},
{
姓名:“乔”,
年龄:45,,
电子邮件:“mike@hotmail.com"
}
];
常量res=u2;.filter(名称,(名称)=>{
返回name.name==“Joe”&&name.age<30;
});
控制台日志(res)代码>
对于希望使用任意键从对象数组中筛选的用户:
函数过滤器项(项,searchVal){
返回items.filter((item)=>Object.values(item.includes(searchVal));
}
让数据=[
{“名称”:“苹果”,“类型”:“水果”,“id”:123234},
{“名称”:“猫”,“类型”:“动物”,“id”:98989},
{“name”:“something”,“type”:“other”,“id”:6565}]
log(“按名称过滤:”,filterItems(数据,“apple”);
log(“按类型过滤:”,filterItems(数据,“动物”);
log(“按id过滤:,filterItems(数据,6565))代码>最简单易读的方法是使用本机javascript过滤方法
原生javaScript过滤器在过滤数组元素时采用声明式方法。因为它是在Array.prototype上定义的方法,所以它在提供的数组上迭代并调用回调。此回调函数作为我们的筛选函数,具有三个参数:
元素
-正在迭代的数组中的当前项
index
-正在迭代的数组中当前元素的索引或位置
数组
-应用筛选方法的原始数组
让我们在一个例子中使用这个过滤器方法。请注意,过滤器可以应用于任何类型的数组。在本例中,我们将根据对象属性过滤对象数组
基于对象属性过滤对象数组的示例如下所示:
// Please do not hate me for bashing on pizza and burgers.
// and FYI, I totally made up the healthMetric param :)
let foods = [
{ type: "pizza", healthMetric: 25 },
{ type: "burger", healthMetric: 10 },
{ type: "salad", healthMetric: 60 },
{ type: "apple", healthMetric: 82 }
];
let isHealthy = food => food.healthMetric >= 50;
const result = foods.filter(isHealthy);
console.log(result.map(food => food.type));
// Result: ['salad', 'apple']
要了解有关函数中筛选数组的更多信息并构建自己的筛选,请参阅本文:
搜索是指过滤?搜索!=过滤器。你想要哪一个?我建议看看链接MDN页面中的polyfill,只是为了有一些有趣的阅读代码和学习一些新的东西,可能。
// Please do not hate me for bashing on pizza and burgers.
// and FYI, I totally made up the healthMetric param :)
let foods = [
{ type: "pizza", healthMetric: 25 },
{ type: "burger", healthMetric: 10 },
{ type: "salad", healthMetric: 60 },
{ type: "apple", healthMetric: 82 }
];
let isHealthy = food => food.healthMetric >= 50;
const result = foods.filter(isHealthy);
console.log(result.map(food => food.type));
// Result: ['salad', 'apple']