0xSingularity
0xSingularity

Reputation: 570

Javascript get object values from corresponding to a list of keys

Hypothetically if we have an object like:

data = {
    a: 3,
    b: 7,
    c: 94,
    d: 854
}

How could I get the values corresponding to a array of keys using vanilla javascript or jQuery? So Something similar to:

var keys = ["a", "d"]
Object.values(data)[keys]

So the output would be an array with the values [3, 854]

Upvotes: 0

Views: 298

Answers (1)

AlexAV-dev
AlexAV-dev

Reputation: 1175

You can use map

const data = {
    a: 3,
    b: 7,
    c: 94,
    d: 854
}

var keys = ["a", "d"]

const result = keys.map(rec => data[rec])

console.log(result)

Upvotes: 4

Related Questions