Can someone provide guidance on writing a function
keyPair(obj1, obj2, key)
? The function should take two objects and a key string as parameters and return an array with the values of the specified key in both objects.
function keyPair(obj1, obj2, key) {
let arr = [];
for (let k in obj1) {
if (k === key) {
arr.push(obj1[key]);
arr.push(obj2[key]);
}
}
return arr;
}
let dog1 = { name: 'Buddy', breed: 'Labrador' };
let dog2 = { name: 'Bailey', breed: 'Golden Retriever' };
console.log(keyPair(dog1, dog2, 'breed')); // [ 'Labrador', 'Golden Retriever' ]
console.log(keyPair(dog1, dog2, 'name')); // [ 'Buddy', 'Bailey ]
I've been struggling with this task, so any help would be appreciated. Thanks!