Write a function `valuePair(obj1, obj2, key)` that takes in two objects
and a key (string). The function should return an array containing the
corresponding values of the objects for the given key.
Examples:
let object1 = {name: 'One', location: 'NY', age: 3};
let object2 = {name: 'Two', location: 'SF'};
valuePair(object1, object2, 'location'); // => [ 'NY', 'SF' ]
valuePair(object1, object2, 'name'); // => [ 'One', 'Two' ]
***********************************************************************
What I have tried:
function valuePair(obj1, obj2, key) {
var in_both = [];
let obj1Entries = Object.entries(obj1);
console.log(obj1Entries)
}
Some reason this is printing:
[ [ 'name', 'One' ], [ 'location', 'NY' ], [ 'age', 3 ] ]
[ [ 'name', 'One' ] [ 'location', 'NY' ], [ 'age', 3 ] ]
So I was going to just loop through arrays and slowly filter them down but with a duplicate I am confused.
There also has to be a better way to do it than:
Creating Array of Key/Value Pairs in each object
Iterating through to find the matching Keys then doing i+1 to push them to new Array.
Right?
The best I've found is something like:
for (var key in objectOne) {
if (Object.hasOwnProperty.call(objectTwo, key)) {
in_both.push(key);
}
}
Which will give me the Keys but not the Values.
Either way, brain is fried from trying to learn Javascript for 12 hours straight. Walking the dog, eating some food then bed for me.