最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to filter array of objects by values in javascript? - Stack Overflow

programmeradmin2浏览0评论

So, i'm having the filtered ids in one array and having the all data's in another array of objects. I want to display filtered ids values mentioned in array of value.

var allData = [
    {
      id:'10',
      name:'jhon'
    },
    {
      id:'11',
      name:'lewis'
    },
    {
      id:'12',
      name:'taylor'
    },
    {
      id:'13',
      name:'adam'
    },
    {
      id:'14',
      name:'bolive'
    }
];

var addedIds = ['10', '12', '14'];

My javascript code,

allData.filter(data, function (item) {
    item.map(function(list, i) {
        if (list.id.indexOf(addedIds[i]) === -1) {
            return;
        }
        console.log(list);
    });
});

So, i'm having the filtered ids in one array and having the all data's in another array of objects. I want to display filtered ids values mentioned in array of value.

var allData = [
    {
      id:'10',
      name:'jhon'
    },
    {
      id:'11',
      name:'lewis'
    },
    {
      id:'12',
      name:'taylor'
    },
    {
      id:'13',
      name:'adam'
    },
    {
      id:'14',
      name:'bolive'
    }
];

var addedIds = ['10', '12', '14'];

My javascript code,

allData.filter(data, function (item) {
    item.map(function(list, i) {
        if (list.id.indexOf(addedIds[i]) === -1) {
            return;
        }
        console.log(list);
    });
});
Share Improve this question edited Oct 26, 2016 at 15:19 Rax Weber 3,78020 silver badges30 bronze badges asked Oct 26, 2016 at 13:40 SathyaSathya 1,7343 gold badges38 silver badges59 bronze badges 4
  • what do you think this addedIds[i] does, and why do you think i is a relevant index in this context? – njzk2 Commented Oct 26, 2016 at 13:43
  • To clarify you want to output the name of each id listed on addedIds ? – DaniP Commented Oct 26, 2016 at 13:43
  • my result should be like this, [{id:"10",name:'jhon' }, {id:"12",name:'taylor' }, {id:"14",name:'bolive' }] – Sathya Commented Oct 26, 2016 at 13:46
  • your filter callback function should return true when a given record should be in the resulting array – William B Commented Oct 26, 2016 at 13:47
Add a ment  | 

5 Answers 5

Reset to default 7

How about this?

Array.prototype.filter only expects one parameter: a function that is passed every item in the array and returns true or false depending on if that item should be included in the filter or not.

You can use Array.prototype.indexOf to see if a string is in an array.

var allData = [{
  id: '10',
  name: 'jhon'
}, {
  id: '11',
  name: 'lewis'
}, {
  id: '12',
  name: 'taylor'
}, {
  id: '13',
  name: 'adam'
}, {
  id: '14',
  name: 'bolive'
}];

var addedIds = ['10', '12', '14'];

var filteredData = allData.filter(function(item) {
  return addedIds.indexOf(item.id) != -1;
});

console.log(filteredData);

var allData = [
    {
      id:'10',
      name:'jhon'
    },
    {
      id:'11',
      name:'lewis'
    },
    {
      id:'12',
      name:'taylor'
    },
    {
      id:'13',
      name:'adam'
    },
    {
      id:'14',
      name:'bolive'
    }
];

var addedIds = ['10', '12', '14'];

var allData = allData.filter(function (item) {   
    if (addedIds.indexOf(item.id) !== -1) return item;
});

console.log(allData);

var allData = [
{
  id:'10',
  name:'jhon'
},
{
  id:'11',
  name:'lewis'
},
{
  id:'12',
  name:'taylor'
},
{
  id:'13',
  name:'adam'
},
{
  id:'14',
  name:'bolive'
}
];

var addedIds = ['10', '12', '14'];

var filteredValues = allData.filter(x => addedIds.indexOf(x.id) !== -1);
console.log(filteredValues);

var allData = [{
  id: '10',
  name: 'jhon'
}, {
  id: '11',
  name: 'lewis'
}, {
  id: '12',
  name: 'taylor'
}, {
  id: '13',
  name: 'adam'
}, {
  id: '14',
  name: 'bolive'
}];

var addedIds = ['10', '12', '14'];
var addedIdsSet = new Set(addedIds);
var result = allData.filter(e => addedIdsSet.has(e.id));
console.log(result)

I personally like turning arrays of Ids into a Set, and moving from there:

var addedIdsSet = new Set(addedIds);

Then the rest is a matter of filtering (like you tried) using the set:

console.log(allData.filter(x => addedIdsSet.has(x.id)))

Note: Using a Set is only really necessary if you have a large list of Ids and want to squeeze out some performance, as this will drop plexity from O(n^2) to O(n), where n is the number of elements in allData. Regardless, I like using a Set anyway because the code ends up looking cleaner too!

You can do it like this:

var allData = [
    {
      id:'10',
      name:'jhon'
    },
    {
      id:'11',
      name:'lewis'
    },
    {
      id:'12',
      name:'taylor'
    },
    {
      id:'13',
      name:'adam'
    },
    {
      id:'14',
      name:'bolive'
    }
];

var addedIds = ['10', '12', '14'];

var filtered_results = [];

allData.filter(function (item) {
  for(var k in addedIds){
      if(item.id === addedIds[k]){
          filtered_results.push(item);
      }
  }
});
console.log(filtered_results);

发布评论

评论列表(0)

  1. 暂无评论