1

I have the following array:

[
    ["123", { keyOne: "val", keyTwo: "val2" }],
    ["402", { keyOne: "val123", keyTwo: "val233" }],
    ["542", { keyOne: "val", keyTwo: "val2" }],
];

And I need to turn it into an Object like this:

{
    "123": { keyOne: "val", keyTwo: "val2" },
    "402": { keyOne: "val123", keyTwo: "val233" },
    "542": { keyOne: "val", keyTwo: "val2" }
}

I think this should be done with reduce() , but I am not sure how.

0

1 Answer 1

4

You can use Object.fromEntries

const data = [
    ["123", { keyOne: "val", keyTwo: "val2" }],
    ["402", { keyOne: "val123", keyTwo: "val233" }],
    ["542", { keyOne: "val", keyTwo: "val2" }],
];

const result = Object.fromEntries(data);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.