[英]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. 您可以使用
filter
和every
方法来执行此操作。
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.filter
和Array.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.