sao
sao

Reputation: 1821

Make a new JS Object using an old one, see description for example

This one is pretty straight forward, I think I just don't know what tool to use. I've got an object which looks like this:

{
  email: "[email protected]", phone: "222-333-4444"
}

i am looking to convert it to the following array with nested objects

[
 {
  name: "email", value: "[email protected]"
 },
 {
  name: "phone", value: "222-333-4444"
 },
]

im familiar with .map() and Oject.keys, just keep running into a wall on this one.

this is what i've been trying but im getting syntax errors

const data = Object.keys(data).map(key => {name: key, value: data[key]});

can anyone help? hopefully some quick points for someone. thanks!

Upvotes: 0

Views: 35

Answers (1)

hgb123
hgb123

Reputation: 14881

To return object from arrow function, you must wrap it in () doc

To return an object literal expression requires parentheses around expression

const data = {
  email: "[email protected]", phone: "222-333-4444"
};

const result = Object.keys(data).map(key => ({name: key, value: data[key]}));

Upvotes: 3

Related Questions