如何通过多个(所有)某个对象的值过滤数组?

时间:2019-02-21 14:50:20

标签: javascript

在下面的代码中,我试图将persons数组仅减少到与filters对象完全匹配的人。

当前,输出是在任何对象键中都具有匹配项的对象数组。如何调整它以仅返回第二个人-与所有过滤条件完全匹配的那个人?

var persons = [
	{city: 'London', 'age': 18, 'name': 'Josh'},
	{city: 'London', 'age':42, 'name': 'Josh'},
	{city: 'New York', 'age': 42, 'name': 'Mike'},
	{city: 'Tokyo', 'age': 33, 'name': 'Kazuna'},
]

var filters = {
	city: 'London',
	age: 42,
	name: 'Josh',
}

function filter(person){
	for (let key of Object.keys(filters)) {
		if (person[key] == filters[key]) {
			return person
		}
	}
}

var filtered = persons.filter((person) => filter(person))
console.log(filtered)

5 个答案:

答案 0 :(得分:5)

您应该使用函数every完成此操作。

var persons = [{     city: 'London',     'age': 18,     'name': 'Josh'   },   {     city: 'London',     'age': 42,     'name': 'Josh'   },   {     city: 'New York',     'age': 42,     'name': 'Mike'   },   {     city: 'Tokyo',     'age': 33,     'name': 'Kazuna'   }, ],
    filters = {city: 'London',age: 42,name: 'Josh'};

function myOwnFilter(person) {
  return Object.keys(filters).every(key => filters[key] === person[key]);
}

var filtered = persons.filter(myOwnFilter);
console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

您可以获取过滤器对象的条目,并使用它来检查每个条目的每个人 Array#every,如果每个谓词为true,则返回true

var persons = [{ city: 'London', 'age': 18, 'name': 'Josh' }, { city: 'London', 'age':42, 'name': 'Josh' }, { city: 'New York', 'age': 42, 'name': 'Mike' }, { city: 'Tokyo', 'age': 33, 'name': 'Kazuna' }],
    filters = { city: 'London', age: 42, name: 'Josh' },
    filterEntries = Object.entries(filters),
    result = persons.filter(person => filterEntries.every(([k, v]) => person[k] === v));

console.log(result);

答案 2 :(得分:1)

使用every()来确保所有内容匹配

var persons = [
	{city: 'London', 'age': 18, 'name': 'Josh'},
	{city: 'London', 'age':42, 'name': 'Josh'},
	{city: 'New York', 'age': 42, 'name': 'Mike'},
	{city: 'Tokyo', 'age': 33, 'name': 'Kazuna'},
]

var filters = {
	city: 'London',
	age: 42,
	name: 'Josh',
}

const result = persons
  .filter(person => 
    Object.keys(filters)
      .every(key => 
        filters[key] === person[key]
      )
  )

console.log(result)

您可以提高将Object.keys()移出循环的性能

答案 3 :(得分:0)

您可以使用&&filter

中创建条件

var persons = [{
    city: 'London',
    'age': 18,
    'name': 'Josh'
  },
  {
    city: 'London',
    'age': 42,
    'name': 'Josh'
  },
  {
    city: 'New York',
    'age': 42,
    'name': 'Mike'
  },
  {
    city: 'Tokyo',
    'age': 33,
    'name': 'Kazuna'
  },
]

var filters = {
  city: 'London',
  age: 42,
  name: 'Josh',
}


var filtered = persons.filter(function(person) {
  return person.city === filters.city && person.age === filters.age && person.name === filters.name

})
console.log(filtered)

答案 4 :(得分:0)

您是否正在寻找类似的东西?

var persons = [
    {city: 'London', 'age': 18, 'name': 'Josh'},
    {city: 'London', 'age':42, 'name': 'Josh'},
    {city: 'New York', 'age': 42, 'name': 'Mike'},
    {city: 'Tokyo', 'age': 33, 'name': 'Kazuna'},
]

var filters = {
    city: 'London',
    age: 42,
    name: 'Josh',
}

function filter(person){
    for (let key of Object.keys(filters)) {
        if (person[key] !== filters[key]) {
            return false;
        }
    }
    return true;
}



var filtered = persons.filter((person) => filter(person))
console.log(filtered)