Wherefore art thou using filter

Wherefore art thou using filter
0.0 0

#1

i want to solve this problem using filter function this is my solution using nested for loops

function whatIsInAName(collection, source) {
 let sourceprop=Object.keys(source);
 let sourcevals=Object.values(source);
  var arr = [];
  let pushit;
 for(let i=0;i<collection.length;i++)
 {
   pushit=true;
   for(let j=0;j<sourceprop.length;j++)
   {
     if(collection[i][sourceprop[j]]!=sourcevals[j]){
       pushit=false;
       break;
     }
     }
   if(pushit)
  arr.push(collection[i]) ;
 }
  return arr;
}
console.clear();
console.log(whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" }));

Now this is how i am trying to solve using filter


function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
arr=collection.filter(el=>el.hasOwnProperty(Object.keys(source))&&el[Object.keys(source)]==Object.values(source));
 
  return arr;
}

i think this is only checking for first property of source object.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/


#2

Explain what you think the following part of the filter function is evaluating.

el[Object.keys(source)] == Object.values(source)

#3

collection is array of objects
*el // is referencing to these objects
*Object.keys(source) //it is an array having the properties of object source.
and similarly
*Object.values(source) // stores the values of object source.
*el[Object.keys(source)] // this will return the value of the property from el object.
el[Object.keys(source)] == Object.values(source)//this is checking
el[name of property of source object] == the value of that property.
m i right?? :sweat_smile:


#4

That part attempts to compare a single value to an array (Object.values(source)). Remember that Object.keys(source) is also an array, so writing el[Object.keys(source)]) makes no sense.

You should add some console.log statements to see what the values are of each before continuing.

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  arr=collection.filter(el=> {
    console.log(el[Object.keys(source)]);
    console.log(Object.values(source));
    console.log();
    return el.hasOwnProperty(Object.keys(source)) && el[Object.keys(source)]==Object.values(source);
  }); 
  return arr;
}

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

The above yields:

undefined
[ 1, 2 ]

undefined
[ 1, 2 ]

undefined
[ 1, 2 ]

[]


#5

OMG :disappointed_relieved: i was comparing it to array instead of its elements .
thank you for your time sir :smile: you are awesome


#6

Finally i did it :relieved: :muscle:


function whatIsInAName(collection, source) {
  var arr = [];
 function check(el)
{
for(let key in source)
{
if(el.hasOwnProperty(key))
{
    if(el[key]===source[key])
    {
      console.log(el[key],key);
    }
    else{return false;}
}
else return false;
}
return true;
}
arr=collection.filter(check);
  return arr;
}

review this please