Search code examples
arraystypescriptecmascript-6angular7

Replacing keys by values according to some predefined object in javascript


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']


Solution

  • Try This

    let x= {'Hello':'Monday', 'World':'Tuesday', 'Program':'Wednesday'}
    let y = ['Program','Hello']
    
    console.log(y.map(val => x[val]));