问题描述
我有一个数组对象:
[
{id:1,name:'Pedro'},
{id:2,name:'Miko'},
{id:3,name:'Bear'},
{id:4,name:'Teddy'},
{id:5,name:'Mouse'}
]
与ids [1,3,5]
如何过滤数组对象留下记录只有从第二个ID?
Thanks!
试试这个( I have an array object: and have array with ids [1,3,5] How to filter array object to leave records only with id's from second one?Thanks! Try this (fiddle): 这篇关于Javascript过滤数组从另一个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
$ $ $ $ $ $ $ $ $ var array = [
{id: 1,name:'Pedro'},
{id:2,name:'Miko'},
{id:3,name:'Bear'},
{id:4, name:'Teddy'},
{id:5,name:'Mouse'}
];
var filterArray = [1,3,5];
var filtered = array.filter(function(item){
return filterArray.indexOf(item.id)!== -1;
});
[
{ id:1, name: 'Pedro'},
{ id:2, name: 'Miko'},
{ id:3, name: 'Bear'},
{ id:4, name: 'Teddy'},
{ id:5, name: 'Mouse'}
]
var array = [
{ id:1, name: 'Pedro'},
{ id:2, name: 'Miko'},
{ id:3, name: 'Bear'},
{ id:4, name: 'Teddy'},
{ id:5, name: 'Mouse'}
];
var filterArray = [1,3,5];
var filtered = array.filter(function(item) {
return filterArray.indexOf(item.id) !== -1;
});