在javascript中基于另一个数组过滤对象数组

时间:2017-10-23 16:38:09

标签: javascript arrays filter

我有一个对象数组

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]

如果idid_filter数组和gender = m

中的任何元素匹配,我想过滤一组人

如何使用过滤功能在javascript中执行此操作?

10 个答案:

答案 0 :(得分:8)

您可以使用 array.filter() ,条件很少,无法获得所需的输出。我也纠正了你的JSON。

var filtered = people.filter(function(item) {
        return id_filter.indexOf(item.id) !== -1 && item.gender==='m';
});

<强>样本

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分: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 - 此处使用+将数字字符串转换为数字

答案 2 :(得分:1)

最简单的过滤方法是使用javascript filter函数,类似于:

people.filter((person) => id_filter.includes(person.id))

答案 3 :(得分:1)

在这种情况下,您可以使用filter和include函数,因为您的id是字符串,因此在使用前需要进行解析。

var result = people.filter((person) => (id_filter.includes(parseInt(person.id)) && person.gender ==='m'))

答案 4 :(得分:0)

在这种情况下,这样做更有意义 所以首先你需要遍历你的people数组 然后你检查一个人ID是否等于你的过滤列表

for(person in people) {
     for(id in id_filter) {
         if(person[id] == id && person[gender] == "m"){

         }
     }
}

答案 5 :(得分:0)

您可以使用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".

注意: id中对象的people属性是字符串,因此您必须向filter提供一个字符串ID数组或转换为id属性到一个数字,然后再传递给includes

答案 6 :(得分:0)

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 :(得分:0)

您可以通过以下代码实现:

var filtered_people = people.filter(function(person){
    return id_filter.includes(person.id) && person.gender === 'm';
});

确保每个人的id都是整数,而不是字符串,如示例所示。否则,includes()函数不匹配。此外,您的people数组存在内部语法问题。所以,最终的代码看起来像这样:

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: "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}
];
var id_filter = [1,4,5,8];
var filtered_people = people.filter(function(person){
    return id_filter.includes(person.id) && person.gender === 'm';
});

我希望这会对你有所帮助。 祝你好运。

答案 8 :(得分:0)

如果您的id_filter大,则需要先将其转换为new Set()。这将允许进行恒定时间查找。然后,您可以使用people迭代.filter()数组,如果其中的集合.has()中的true且性别等于{{1 }}。

id

总体而言,此方法的时间复杂度为'm',与使用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)方法时得到的O(Nk)相反,其中{ {1}}是.includes()数组的长度,而.indexOf()N数组的长度

答案 9 :(得分:-1)

    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 );