Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

javascript - 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!

question from:https://stackoverflow.com/questions/65928090/make-a-new-js-object-using-an-old-one-see-description-for-example

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...