Sure, but we need to understand what to compare before we can know how to compare it correctly.
We need to extract the key from source and compare the value of source[key] with the value of collection[key], ain’t that right?
Why blurt out a process with a bunch of technical words when we’re trying to help OP develop his own plan of attack?
Well, as a first step I could find out if source is present in collection by running collection.indexOf(source) … right?
I’m a child. I don’t know what indexOf means.
Without using big fancy code words or scary syntax I don’t understand, how do you want me to look at a list of people and copy every person who has a certain last name onto a new list?
Carry the last name in source and match it to every person in the list.
If the last name matches a name in the list, then take the last name and first name.
Bring the last name and first name back to me.
Ok, small twist.
The list now has more people, and it lists their first name, last name, and favorite color.
How do you want me to copy out a list of everyone with the last name ‘Austin’ who has the favorite color ‘green’?
This small change is the hard part. If you can explain to me, the dumb child who whats to screw it up, how to do this, you’re close to being able to tell the computer.
Give child “Green” and “Austin”. Every time the child finds Austin and Green, the child writes down the first name. When finished with the list, child brings list of new names to me.
Finds how? I need very specific directions.
If I have lines like
first: Bob, last: Austin, color: orange
How do I know if the line should be copied?
If the line has Austin, then the line should be copied. If not, then ignore the line
But you left out the color requirement.
If the line has Austin
{city: "Austin"}
{Austin: true}
Both have Austin, is that the kind of objects you want?
Check each line for the last name Austin and the color green.
If a line has both the last name Austin and the color green, write down all of the information in the line. If it does not have “Austin” or “green”, ignore it and move on to the next line. When you are finished with the entire list, bring it back to me.
So this is the hard part of the challenge right here. Let’s finally think code.
The list is an array. I’ve seen you iterate over arrays before. So let’s ignore that for a second.
How if I give you two objects, objectA and objectB, how can you check that objectA has all of the same properties as ObjectB and set to the same value? This is what checking that the person has the matching last name and favorite color means in code.
let newVal = ObjectA.hasOwnProperty(objectB)
This is all I can think of, right now.
The argument provided to .hasOwnProperty() has to be a key - not an object.
let objectA={name: "Tom", toy:"ball"}
let objectB={name:"Tom"}
objectA.hasOwnProperty("name") == true
objectA.hasOwnProperty("age") == false
objectA.hasOwnProperty(objectB) == false // appearently doesn't throw an error or undefined, but is always false
Right. What about this?
objectA.hasOwnProperty(Object.keys(objectB)
This can’t work. .hasOwnProperty() takes a string. An object is not a string.
There isn’t some magic method to do this for you. You’re going to need to write the logic yourself.
Guessing syntax isn’t going to help you here. There isn’t a special function you can guess that will do the work for you. You need to create the logic that describes what you want to happen.
Looking at the example @Jagaya gave above and expanding it:
let objectA = {name: "Tom", toy: "ball"};
let objectB = {name: "Tom"};
You need some process to describe if objectA has the property name and that property is set to "Tom".
You need a process because you can’t possibly hardcode the property names and you don’t know ahead of time how many different properties are in ObjectB:
let objectA={name: "Tom", toy:"ball", birthday: "1 Jan 2000", pet: "cat", color: "blue"};
let objectB={name: "Tom", color: "blue", pet: "fish"};
Closer, but Object.keys() returns an array and an array cannot be a key.
let objectA={name: "Tom", toy:"ball"}
let objectB={name:"Tom"}
// Object.keys(objectB) -> ["name"]
objectA.hasOwnProperty(["name"]) == true
let objectC={name: "Tom", toy:"ball"}
// Object.keys(objectC) -> ["name", "toy"]
objectA.hasOwnProperty(["name", "toy"]) == false
Only works if there is only one key. But the tests include several keys to check, so you gotta figure that one out.