0

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"}....
5
  • console.log(Object.values(o).map(v => `{"name": "${v}" }`).join(", ")); but i think the question went right over my head. Commented Jan 30, 2020 at 17:58
  • What do you mean with "log out". Are you only interested in an output string, not an object structure? Commented Jan 30, 2020 at 18:00
  • Thanks @tirincot ! I mean log out to output but I would eventually wanna use it so maybe store it in a variable... Commented Jan 30, 2020 at 18:01
  • Object.values(o).map(v => ({name: v})) Commented Jan 30, 2020 at 18:02
  • So, is the input a single object, and the result an array of objects? It would be nice if you would present the expected result as a JavaScript object/array literal. Like the above suggestions could work, or Object.values(o).map(name => ({name})) Commented Jan 30, 2020 at 18:02

1 Answer 1

3

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.