简体   繁体   中英

Javascript: Add Key to an Multiple Object of Values

I have this object that log out as in:

{0: "16407", 1: "16309", 2: "16308", 3: "16307", 4: "16306", 5: "16305", 6: "16303", 7: "16302", 8: "16301", 9: "16300", 10: "16299", 11: "16298", 12: "16297", 13: "16296", 14: "16294", 15: "16293", 16: "16292", 17: "16290", 18: "16285", 19: "16277"}

How can I restructure this to log out as follows:

{"name": "16407" }, {"name": "16309"}, {"name": "16308"}....

You could move the values into an array and map objects.

 var object = { 0: "16407", 1: "16309", 2: "16308", 3: "16307", 4: "16306", 5: "16305", 6: "16303", 7: "16302", 8: "16301", 9: "16300", 10: "16299", 11: "16298", 12: "16297", 13: "16296", 14: "16294", 15: "16293", 16: "16292", 17: "16290", 18: "16285", 19: "16277" }, result = Object.assign([], object).map(name => ({ name })); console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM