使用javascript或jquery从数组中使用多个值来过滤数组对象

时间:2017-09-07 04:28:44

标签: javascript jquery

行用户数组看起来像。

var users = [{name: 'John',address: 'USA',year:2010 },{name: 'John',address: 'USA',year:2011 },{name: 'John',address: 'USA',year:2012 },{name: 'John',address: 'USA',year:2013 },{name: 'John',address: 'USA',year:2014}];

过滤器数组是,

var filter_year = ['2010','2011','2012'];

我如何在filter_year中使用years获取过滤结果数组,我需要获得结果数组。

var users = [{name: 'John',address: 'USA',year:2010 },{name: 'John',address: 'USA',year:2011 },{name: 'John',address: 'USA',year:2012 }];

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码获取指定年份的所有用户。

ID

答案 1 :(得分:0)

只需根据其他数组中的包含过滤一个数组

var users = [{name: 'John',address: 'USA',year:2010 },{name: 'John',address: 'USA',year:2011 },{name: 'John',address: 'USA',year:2012 },{name: 'John',address: 'USA',year:2013 },{name: 'John',address: 'USA',year:2014}];

var filter_year = ['2010','2011','2012'];

var result = users.filter( item => filter_year.includes(item.year+""));

console.log(result)

请注意,年份是一个数组中的整数,另一个是

中的字符串