0

I have an object having key-value pairs. Another array has only a partial set of keys. I want a third array that contains only values and that too only for those keys which are present in the second array.

let x= {'Hello':'Monday', 'World':'Tuesday', 'Program':'Wednesday'}
let y = ['Program','Hello']

What I require in output is : y=['Wednesday', 'Monday']

2
  • Is anything stopping you from achieving what you require? Please note that SO is not a get code for free site. We solve problems of our fellow developers... Commented Oct 17, 2019 at 13:09
  • Possible duplicate of Dynamically access object property using variable Commented Oct 17, 2019 at 13:13

2 Answers 2

2

Try This

let x= {'Hello':'Monday', 'World':'Tuesday', 'Program':'Wednesday'}
let y = ['Program','Hello']

console.log(y.map(val => x[val]));
Sign up to request clarification or add additional context in comments.

3 Comments

Please add explanation. Just code is incomplete answer
code was quite obvious. So didn't feel a need of explanation. Its just a map function of array.
If it was that obvious, OP would have not asked. Remember, you are answering for readers and they have all levels. So its always better to explain what and why.
0

If I understand you correctly, you want to make sure that the result contains only existing values. If so, you need to loop through y values and make sure the x object has a such property.

let x = {'Hello': 'Monday', 'World': 'Tuesday', 'Program': 'Wednesday'},
    y = ['Program', 'Hello', 'Test'],
    z = [];

for (let prop of y) {
  if (prop in x) {
    z.push(x[prop]);
  }
}

console.log(z);

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.