Wherefore art thou - got a couple to pass tried for a while feeling overwhelmed

Tell us what’s happening:
so ive spent a good 12hours or maybe more on this including breaks learnt alot of object methods trying to find a soulation it works for a couple of cases if i change the hasownpro(obj) to include obj[0] it passes for an extra which suggests i need some form of loops for it also when i console.log soruce[obj] on any with more than one case in the source it comes up as undefined kinda burnt out and intrested to know if im going in the right direction at this point i kinda think ive over thought it and should just give up cause when i think about it im just confusing myself the heat doesnt help.

 return x[obj] === source[obj] && x.hasOwnProperty(obj) 

i dont think this line of code is bad however the source[obj] coming as undefined on any with more than 1 key pair i dont know how to tackle this so it will work or is this bit just completely wrong and i should find another way to access the value. i dont know im feeling pretty stuck

Your code so far

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

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

filter array based on value pairs 


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0.

Link to the challenge:

1 Like

The challenge you’re encountering is, Object.keys() returns an array of keys. So rather than checking if x[obj] is a thing, because it never will be, you want to iterate over that array of keys.

Try this logic:

  • create a variable for the Object keys (as you’ve done)
  • filter that collection into an array (as you’re doing), using the following logic:
    • for each key in the Object keys variable, check if this member of the collection has this key AND that the member’s value matches the source object’s value.
      • if it does not, return false. (this will stop this iteration of the filter, and will drop this member).
      • If it DOES, just continue to the next Object key.
    • after all Object keys have been checked, if we are still in the filter function, then they all matched, both having the property and having the right value. Aces! return true to end the filter function and keep this one!
  • At this point, we’ve filtered the collection, and arr should contain the proper ones. I think. So do something with arr, and off we go.

I think that sums up the logic you’re looking for, now the coding bit is all on you. The logic can be challenging, as there are two different arrays you’re needing to figure out how to compare - but once you see the idea behind it, it actually makes sense.

1 Like

thank you after i while i went back to write psudo code and felt so muddled by then i cudnt think straght ill try with this as a template 2night when the weather isnt so damn hot! <3

1 Like