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;
   }
Peter Peach
  • 159
  • 10
  • Possible duplicate of [Javascript: How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes) – mpm Oct 01 '18 at 13:51

6 Answers6

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))
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • Note that this answer, unlike others, doesn't assume any key values and will work for any second argument – Steve Archer Oct 01 '18 at 13:56
  • `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. – 4castle Oct 01 '18 at 13:59
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);
Leonid Pyrlia
  • 1,594
  • 2
  • 11
  • 14
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");
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)

Badrush
  • 1,247
  • 1
  • 17
  • 35
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);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
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;
}
Osama
  • 2,912
  • 1
  • 12
  • 15