0

hey guys so I'm trying to solve this problem here

Make a function that looks through a list (first argument) and returns an array of all objects that have equivalent property values (second argument).

but i can't seem to loop thru an array of objects if my life depended on it..... Here is my following code

function where(collection, source) {
  for(var prop in collection){
    if(collection.hasOwnProperty(prop)){
      console.log("collection." + prop + "=" + collection[prop]);
    }
  }
}

where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' });

i was reading documents on hasOwnProperty method and for in loops but it seems like I am utilizing them wrong, any guidance is appreciated, thanks.

THIS IS NOT A DUPLICATE

7
  • 1
    Dont you need to use 'source' somewhere in your code? Commented Jul 25, 2015 at 4:54
  • @WandMaker im not following you.... Commented Jul 25, 2015 at 4:55
  • 1
    Second parameter of 'where' method, how are you using it? Commented Jul 25, 2015 at 4:56
  • @WandMaker i havent gotten that far yet, I'm still struggling on how to iterate over an array of objects and print their keys and properties.....once i do that, thats a whole other ball park... Commented Jul 25, 2015 at 4:59
  • This question has been answered here - stackoverflow.com/questions/3010840/… Commented Jul 25, 2015 at 5:12

2 Answers 2

1

The for...in loop is not what you want for iterating over arrays in javascript.

Use the sequential for loop to loop through the array, and the for..in loop to iterate over the keys of the object...

for(var i=0; i<collections.length; i+=1) {
  for(var prop in collections[i]){
    if(collections[i].hasOwnProperty(prop)){
      console.log("collection." + prop + "=" + collections[i][prop]);
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

so i just tested your code and i get the following console log collection.last=undefined ....
this looks good, I was told to use a for in loop, for this kind of stuff..... thanks for clarifying for me!
1

One can use filter method to filter out elements that satisfy the condition. Also, since comparison of objects is needed in evaluating the condition, and there does not seem to be an equals method in JavaScript for Objects, one has to do additional work to check equality.

Here is one way the problem mentioned in question can be solved

function where(collection, source) {
  return collection.filter(function(item) { 
     return Object.keys(source).every(function(k){ return item[k] == source[k] });
  });
}

console.log (where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet'}));

Output

[ { first: 'Tybalt', last: 'Capulet' } ]

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.