0

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

This is what I have come up with but don't know what to put in the for...in loop:

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

6 Answers 6

4

You can do this with filter and every methods.

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))

Sign up to request clarification or add additional context in comments.

2 Comments

Note that this answer, unlike others, doesn't assume any key values and will work for any second argument
k in o would be better than checking o[k] for a truthy value, especially in the event that 0 or an empty string is a possible value.
1

A simple ES6 version with Array.prototype.filter and Array.prototype.every:

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);

Comments

0

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");

Comments

0

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).

var newArray = findByAge(myArr, obj)

Comments

0

You need to put an if condition comparing the age value of your check object with the age value of the list object. In case, both the values are equal, push object in 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.

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);

Comments

0
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.