Click here to Skip to main content
15,846,144 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.

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) {
  // Your code here
  var in_both  = [];

  let obj1Entries = Object.entries(obj1);



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.

The best I've found is something like:

for (var key in objectOne) { // simply iterate over the keys in the first object
    if (, key)) { // and check if the key is in the other object, too

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.
Updated 6-Oct-22 0:29am
Richard MacCutchan 6-Oct-22 5:08am    
Look at the question again. You are to take the values of each object that corresponds to the key, and store those in the array. In your sample above you only look at obj1.
Chris Aug2022 6-Oct-22 5:20am    
It's 5:15am and at this point I am going to go to bed. The key here is I have no idea how to complete this in an efficient way, even if I Object.Entries obj1 + obj2 - some reason when I console.log(obj1) I am getting duplicates as shown above so even if I wrote code to sort through the array to match the location, pluck out the key 'NY' and then do the same for Obj2 aka 'SF' I would end up with something like [NY, SF, NY, SF] and then further have to remove duplicates.

What I am looking for is an answer so I can deconstruct it and learn because when I research on Google or StackOverflow I just keep getting sent down rabbit holes.

Regardless, I am going to walk the dog, eat and then go to sleep because my brain is fried at this point from trying to learn Javascript for the last 12 hours.
Richard MacCutchan 6-Oct-22 5:29am    
See below.

1 solution

I have no idea what the above question is about or where you got it from. But if you are trying to learn a language then start with a proper well-written tutorial: JavaScript Tutorial[^].

Having looked again at the above tutorial, I think that all you need is:
function valuePair(obj1, obj2, key) {
  var in_both = [];



Seems to work in my limited testing.
Share this answer
Chris Aug2022 6-Oct-22 6:13am    
Then don't bother commenting on the question. And I'm following a tutorial App Academy Open and this is one of the last questions before I complete the bigger Objects Section.
Richard MacCutchan 6-Oct-22 6:19am    
Some people just do not want to be helped.
Chris Aug2022 6-Oct-22 6:27am    
THat's because your original answer only contained:

I have no idea what the above question is about or where you got it from. But if you are trying to learn a language then start with a proper well-written tutorial: JavaScript Tutorial[^].

-------------------- Already told you I had been doing this stuff for 12 hours straight and am falling asleep sitting up so I clearly over complicated it. I know how to loop through objects, assign keys/values etc and skipped this one and am on immensley harder questions at this point before sleep like looping through an array and counting how many times a specific word appears in it then creating an object with the word and the word count for multiple words in key/value pairs of word:count.

Not hard for you, but for someone on their first day of objects it is. Again, I clearly over complicated this one question before moving on and came here for actual help, not a reference to a tutorial.
Richard MacCutchan 6-Oct-22 6:42am    
"Already told you I had been doing this stuff for 12 hours straight"
Yes. but without any explanation of what learning materials you were working with, apart from Google. Hence my suggestion to use a good tutorial, which actually has good working examples of most of the things you want to learn.
Chris Aug2022 6-Oct-22 21:29pm    
When answering a question please:

Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome. -----------------------------------------------------------------------------See Below---------------------------------
Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900