简体   繁体   English

如何使用另一个对象中的值过滤对象数组?

[英]How can I filter an array of objects with a value from another object?

I want to write a function which takes an array of objects with certain key value pairs as the first argument. 我想编写一个函数,该函数将具有某些键值对的对象数组作为第一个参数。 And an object with key value pairs as the second argument. 还有一个具有键值对作为第二个参数的对象。

The function should check if the key value pairs from the second argument are found in the array of objects from the first argument. 函数应检查是否在第一个参数的对象数组中找到了第二个参数的键值对。

If so, it should return an array of objects which have the matching name and value pairs. 如果是这样,它将返回具有匹配的名称和值对的对象数组。

For example, if I have an array of objects (first argument): 例如,如果我有一个对象数组(第一个参数):

[{name: "Peter", age: 21}, {name: "Kate", age: 18}, {name: "Tihon", age: 17}, {name: "Poopy", age: 17}]

And as the second argument: 作为第二个参数:

{age: 17}

It should return: 它应该返回:

[{name: "Tihon", age: 17}, {name: "Poopy", age: 17}]

Because of the matching value age 由于匹配age价值

This is what I have come up with but don't know what to put in the for...in loop: 这是我想出的,但不知道在for...in循环中放入什么:

   function checkTheName(list, check) {
     let newArr = [];
     for(let i = 0; i < list.length; i++){
        for(let key in list[i]){
            // Stuck here
        }
     }
     return newArr;
   }

You can do this with filter and every methods. 您可以使用filterevery方法来执行此操作。

 let a = [{name: "Peter", age: 21}, {name: "Kate", age: 18}, {name: "Tihon", age: 17}, {name: "Poopy", age: 17}] let b = {age: 17} function checkTheName(list, check) { return list.filter(o => Object.keys(check).every(k => { return (k in o) && check[k] == o[k] })) } console.log(checkTheName(a, b)) 

A simple ES6 version with Array.prototype.filter and Array.prototype.every : 具有Array.prototype.filterArray.prototype.every简单ES6版本:

 const data = [{name: "Peter", age: 21}, {name: "Kate", age: 18}, {name: "Tihon", age: 17}, {name: "Poopy", age: 17}]; const fObj = {age: 17}; const filtred = data.filter(item => Object.keys(fObj).every(k => item.hasOwnProperty(k) && item[k] === fObj[k]) ); console.log(filtred); 

You can loop over the array and test for that property: 您可以遍历数组并测试该属性:

function checkTheName(check, list){
    for (var i=0; i < myArray.length; i++) {
        if (myArray[i].name === nameKey) {
            return myArray[i];
        }
    }
}

var array =[{name: "Peter", age: 21}, {name: "Kate", age: 18}, {name: "Tihon", 
age: 17}, {namenter code heree: "Poopy", age: 17}]
;

var resultObject = checkTheName( array,"string 1");

Use filter to loop over the array. 使用过滤器遍历数组。

function findByAge(myArr, obj){
    myArr.filter( (item) => {
       if(obj.age === item.age){
         return item
       }
    })
}

This will return an array with just the array items that you are looking for. 这将返回仅包含您要查找的数组项的数组。

You can call it following line. 您可以在下一行称呼它。 Since the function returns a new array. 由于该函数返回一个新数组。 We need to give the new array a name (newArray in this example). 我们需要给新数组起一个名字(在本例中为newArray)。

var newArray = findByAge(myArr, obj)

You need to put an if condition comparing the age value of your check object with the age value of the list object. 你需要把一个if条件比较age你的值check与对象age的值list对象。 In case, both the values are equal, push object in newArr . 如果两个值相等,则在newArr推送对象。

 let list = [{ name: "Peter", age: 21 }, { name: "Kate", age: 18 }, { name: "Tihon", age: 17 }, { name: "Poopy", age: 17 }], check = { age: 17 }; function checkTheName(list, check) { let newArr = []; for (let i = 0; i < list.length; i++) { if (list[i].age == check.age) { newArr.push(list[i]); } } return newArr; } console.log(checkTheName(list, check)); 

Alternatively, you can also use array#filter . 另外,您也可以使用array#filter

 let list = [{name: "Peter", age: 21}, {name: "Kate", age: 18}, {name: "Tihon", age: 17}, {name: "Poopy", age: 17}], check = {age: 17}, result = list.filter(o => o.age === check.age); console.log(result); 

var filterobj ={age:17};
var data=[{name: "Tihon", age: 17}, {name: "Poopy", age: 17}]
 var newArray = data.filter(function (el) {
 return el.age ==filterobj.age;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我从 React 中的另一个数组对象创建的数组中进行过滤? - How can I filter from an Array that i created from another array objects in React? 如何使用基于另一个数组的值过滤对象数组 - How to filter array of objects with based on value from another array 如何在比较另一个对象的同时过滤对象数组? - How can I filter through an array of objects while also comparing another object? 如何根据 Angular 中另一个对象的属性过滤一组对象? - How to filter an array of objects based of attribute from another object in Angular? 如果过滤键可以有任何类型的值,我如何过滤数组中的唯一对象? - How do I filter the unique objects from an array if the filter key can have any type of value? 如何从按一个值过滤的对象数组创建一个新数组? - How can I create a new array from an array of objects filter by one value? 如何排序 / map / 过滤以下 javascript 对象数组从一种格式到另一种格式 - How can I sort / map / filter the following javascript array of objects from one format to another 如何过滤作为对象数组中对象属性的电子邮件域? - How can I filter an email domain that is a property of an object that is in an array of objects? 如何通过以下方式过滤对象数组:如果对象 ID 是连续的 - How can I filter the array of objects by: if object ids are consective 如果对象包含值存在于另一个数组中的属性,则过滤对象数组 - Filter array of objects if object contains attribute with value present in another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM