基于javascript中的另一个数组过滤对象数组
Posted
技术标签:
【中文标题】基于javascript中的另一个数组过滤对象数组【英文标题】:Filter array of objects based on another array in javascript 【发布时间】:2018-04-04 07:06:43 【问题描述】:给定一个对象数组:
people = [
id: "1", name: "abc", gender: "m", age:"15" ,
id: "2", name: "a", gender: "m", age:"25" ,
id: "3", name: "efg", gender: "f", age:"5" ,
id: "4", name: "hjk", gender: "m", age:"35" ,
id: "5", name: "ikly", gender: "m", age:"41" ,
id: "6", name: "ert", gender: "f", age:" 30" ,
id: "7", name: "qwe", gender: "f", age:" 31" ,
id: "8", name: "bdd", gender: "m", age:" 78" ,
]
还有一个想要的 id 数组:
id_filter = [1,4,5,8]
如何过滤数组people
以返回id_filter
和gender=m
中定义的目标ID?
【问题讨论】:
请使用const
或let
来声明变量,例如const people = []
。
【参考方案1】:
在这种情况下,步行更有意义
所以首先你需要遍历你的 people
数组
然后你检查一个人的 id 是否等于你的过滤器列表
for(person in people)
for(id in id_filter)
if(person[id] == id && person[gender] == "m")
【讨论】:
我想要一个过滤数组,而且 id 不是串行的。还需要添加另一个条件,即性别 = m【参考方案2】:最简单的过滤方法是使用数组的filter()
函数,类似这样:
people.filter(person => id_filter.includes(person.id))
【讨论】:
如果我想添加另一个条件,即性别应该是 == m 怎么办?people.filter((person) => person.gender == 'm')
真的是people.filter((person) => id_filter.includes(Number(person.id)) && person.gender === "m")
或people.filter(( id, gender ) => id_filter.includes(Number(id)) && gender === "m")
。【参考方案3】:
people = [
id : "1", name : "abc", gender : "m", age :"15" ,
id : "2", name : "a", gender : "m", age :"25" ,
id : "3", name : "efg", gender : "f", age :"5" ,
id : "4", name : "hjk", gender : "m", age :"35" ,
id : "5", name : "ikly", gender : "m", age :"41" ,
id : "6", name : "ert", gender : "f", age :" 30" ,
id : "7", name : "qwe", gender : "f", age :" 31" ,
id : "8", name : "bdd", gender : "m", age :" 78"
]
var id_filter = ["1","4","5","8"], filteredPeople = [];
for( var i=people.length-1; i>=0; --i)
if( id_filter.indexOf( people[i].id ) != -1 )
filteredPeople.push( people[i] );
console.log( filteredPeople );
【讨论】:
【参考方案4】:带有Array.includes()
功能:
var people = [
id : "1", name : "abc", gender : "m", age :"15" , id : "2", name : "a", gender : "m", age :"25" ,
id : "3", name : "efg", gender : "f", age :"5" , id : "4", name : "hjk", gender : "m", age :"35" ,
id : "5", name : "ikly", gender : "m", age :"41" , id : "6", name : "ert", gender : "f", age :" 30" ,
id : "7", name : "qwe", gender : "f", age :" 31" , id : "8", name : "bdd", gender : "m", age :" 78"
],
id_filter = [1,4,5,8],
result = people.filter((o) => id_filter.includes(+o.id) && o.gender == "m");
console.log(result);
+o.id
- +
在这里用于将数字字符串转换为数字
【讨论】:
【参考方案5】:您可以使用Array.prototype.filter
喜欢:
function filter(arr, ids, gender) // takes an array of people arr, an array of indexes ids, and a gender and return the matched people objects from arr
return arr.filter(function(obj) // filtering each object...
return ids.includes(obj.id) && obj.gender === gender; // if this object is is included in the ids array and if its gender property is equal to the desired gender
);
var people = [id:"1",name:"abc",gender:"m",age:"15",id:"2",name:"a",gender:"m",age:"25",id:"3",name:"efg",gender:"f",age:"5",id:"4",name:"hjk",gender:"m",age:"35",id:"5",name:"ikly",gender:"m",age:"41",id:"6",name:"ert",gender:"f",age:"30",id:"7",name:"qwe",gender:"f",age:"31",id:"8",name:"bdd",gender:"m",age:"78"];
console.log(filter(people, ["5", "7", "4"], "m")); // filtering elements where id is one of ["5", "7", "4"] and the gender is "m".
注意:people
中对象的id
属性是字符串,因此您必须向filter
提供字符串ID 数组或将id
属性转换为传递给includes
之前的编号。
【讨论】:
【参考方案6】:const people = [
id: "1", name: "abc", gender: "m", age: "15" ,
id: "2", name: "a", gender: "m", age: "25" ,
id: "3", name: "efg", gender: "f", age: "5" ,
id: "4", name: "hjk", gender: "m", age: "35" ,
id: "5", name: "ikly", gender: "m", age: "41" ,
id: "6", name: "ert", gender: "f", age: " 30" ,
id: "7", name: "qwe", gender: "f", age: " 31" ,
id: "8", name: "bdd", gender: "m", age: " 78" ,
]
const idFilter = [1,4,5,8]
const idIsInList = id => idFilter.includes(+id) // "+id" to make sure it is a number, not a string
const genderIsMale = gender => gender === "m"
const result = people.filter(item => idIsInList(item.id) && genderIsMale(item.gender))
console.log(result)
【讨论】:
【参考方案7】:您可以使用 array.filter()
在几个条件下获得您想要的输出。我也更正了你的 JSON。
var filtered = people.filter(function(item)
return id_filter.indexOf(item.id) !== -1 && item.gender==='m';
);
演示
var people =[
"id": 1, "name": "abc", "gender": "m","age": "15" ,
"id": 2, "name": "a", "gender": "m", "age": "25" ,
"id": 3,"name": "efg", "gender": "f","age": "5" ,
"id": 4,"name": "hjk","gender": "m","age": "35" ,
"id": 5, "name": "ikly","gender": "m","age": "41" ,
"id": 6, "name": "ert", "gender": "f", "age": "30" ,
"id": 7, "name": "qwe", "gender": "f", "age": "31" ,
"id":8, "name": "bdd", "gender": "m", "age": " 8"
];
var id_filter = [1,4,5,8];
var filtered = people.filter(function(item)
return id_filter.indexOf(item.id) !== -1 && item.gender==='m';
);
console.log(filtered);
【讨论】:
【参考方案8】:您可以通过以下代码实现:
const filtered_people = people.filter(function(person)
return id_filter.includes(person.id) && person.gender === 'm';
);
只要确保每个人的 id 是一个整数而不是一个字符串,就像你的例子一样。否则,includes() 函数将不匹配。此外,您的 people
数组存在内部语法问题。所以,最终的代码应该是这样的:
const people = [
id: 1, name: "abc", gender: "m", age:15,
id: 2, name: "a", gender: "m", age: 25,
id: 3, name: "efg", gender: "f", age: 5,
id: 4, name: "hjk", gender: "f", age: 35,
id: 5, name: "ikly", gender: "m", age: 41,
id: 6, name: "ert", gender: "f", age: 30,
id: 7, name: "qwe", gender: "f", age: 31,
id: 8, name: "bdd", gender: "m", age: 78
]
const id_filter = [1,4,5,8]
const filtered_people = people.filter((person) => id_filter.includes(person.id) && person.gender === 'm')
console.log(filtered_people)
我希望这对你有帮助。 祝你好运。
【讨论】:
【参考方案9】:对于这种情况你可以使用filter和include函数,因为你的id是字符串,使用前需要解析。
var result = people.filter((person) => (id_filter.includes(parseInt(person.id)) && person.gender ==='m'))
【讨论】:
【参考方案10】:如果您的id_filter
很大,您需要先将其转换为new Set()
。这将允许恒定时间查找。然后,您可以使用 .filter()
迭代您的 people
数组,如果您在其中设置了 .has()
id
并且性别等于 'm'
,则返回 true
。
const people = [ id: "1", name: "abc", gender: "m", age:"15" , id: "2", name: "a", gender: "m", age:"25" , id: "3", name: "efg", gender: "f", age:"5" , id: "4", name: "hjk", gender: "m", age:"35" , id: "5", name: "ikly", gender: "m", age:"41" , id: "6", name: "ert", gender: "f", age:" 30" , id: "7", name: "qwe", gender: "f", age:" 31" , id: "8", name: "bdd", gender: "m", age:" 78" , ];
const id_filter = new Set([1,4,5,8]);
const res = people.filter((id, gender) => id_filter.has(+id) && gender === 'm');
console.log(res);
总体而言,这种方法的时间复杂度为O(N + k)
,而不是使用.includes()
或.indexOf()
方法时得到的O(Nk)
,其中N
是@ 的长度987654337@数组,k
是id_filter
数组的长度
【讨论】:
以上是关于基于javascript中的另一个数组过滤对象数组的主要内容,如果未能解决你的问题,请参考以下文章
Mongoose- 将数组中的数据搜索/过滤到具有特定项目的另一个数组中