Stag
Stag

Reputation: 53

Array of dictionaries convert to array of values only

I know it might be a simple task but i couldn't find proper solution for my task.

In JavaScript i have a data that's stored in array of dictionaries.

Example:

[{"car": "volvo", "model":"s40"},{"car": "audi", "model": "a4"}]

how i can convert this to get only values out and store them in to the array of arrays?

result:

[["volvo","s40"],["audi","a4"]]

Upvotes: 0

Views: 469

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386560

You could map the values from the objects directly with Array#map and Object.values.

const
    data = [{ car: "volvo", model: "s40" }, { car: "audi", model: "a4" }],
    values = data.map(Object.values);

console.log(values);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

cmgchess
cmgchess

Reputation: 10247

you are looking for the values of each Object so a map and Object.values

let x = [{"car": "volvo", "model":"s40"},{"car": "audi", "model": "a4"}]
let y = x.map(e => Object.values(e))
console.log(y)

Upvotes: 2

Related Questions