repl.it
@QuemuelVital/

Find Matching Object

Nodejs

Make a function that looks through an array of objects (first arg) and returns an array of all objects that have matching name and value.

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function whatIsInAName(collection, source) {
 
  var key = Object.keys(source);
  var arr = [];
  
  for ( let i = 0; i < collection.length; i++ ){
    var boo = true; 
    for ( let j = 0; j < key.length; j++ ){
        if ( !collection[i].hasOwnProperty(key[j] ) || source[key[j]] !== collection[i][key[j]] ){
        boo = false;
      }
    }

    if(boo==true){
      arr.push(collection[i]);
    }

  }
    
  return arr;

}


whatIsInAName
(
  [
    { "apple": 1, "bat": 2 },
    { "bat": 2 },
    { "apple": 1, "bat": 2, "cookie": 2 }
  ], 
{ "apple": 1, "bat": 2 }
);