-3

How do I take a javascript array

var array = [1,2,3,4,5,6]

And convert it to a JSON object that looks like this

[
  {
    "value": 1
  },
  {
    "value": 2
  },
  {
    "value": 3
  },
  {
    "value": 4
  },
  {
    "value": 5
  },
  {
    "value": 6
  }
]

The closest I've come is using this

JSON.stringify(Object.assign({}, array)); 

But it gives me an output like this

{"0":1,"1":2,"2":3,"3":4,"4":5,"5":6}
4

1 Answer 1

1

You could map the values by taking a short hand property.

var array = [1, 2, 3, 4, 5, 6],
    result = array.map(value => ({ value }));

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.