Home > Mobile >  Array of dictionaries convert to array of values only
Array of dictionaries convert to array of values only

Time:03-11

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

CodePudding user response:

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)

CodePudding user response:

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; }

  • Related