0

How to convert the below json into

var body = [{id:1,Country:'Denmark',price: 7.526,Location:'Copenhagen'},
    {id:2, Country:'Switzerland', price:7.509, Location:'Bern'},
  ]

From above json into array of array as like below.

varbody = [
    [1, 'Denmark', 7.526, 'Copenhagen'],
    [2, 'Switzerland', 7.509, 'Bern'],
    [3, 'Iceland', 7.501, 'Reykjavík'],
    [4, 'Norway', 7.498, 'Oslo'],
  ]
3
  • 1
    Please take note that this is not a JSON, but a Javascript Object. JSON are strings Commented Oct 11, 2020 at 9:44
  • @Cid: "JSON are strings" - could you please elaborate? What is JSON in first place? How is it a string? Commented Oct 11, 2020 at 10:19
  • @MichaelD stackoverflow.com/questions/3975859/… Commented Oct 11, 2020 at 10:21

2 Answers 2

1

You could destructure the objects and get an array with the wanted order of the elements.

const
    body = [{ id: 1, Country: 'Denmark', price: 7.526, Location: 'Copenhagen' }, { id: 2, Country: 'Switzerland', price: 7.509, Location: 'Bern' }],
    result = body.map(({ id, Country, price, Location }) => [id, Country, price, Location]);
  
console.log(result);

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

Comments

1

Use the Array#map to map each object and Object#values to transform each object to the array of the object's values:

const body = [{id:1,Country:'Denmark',price: 7.526,Location:'Copenhagen'}, {id:2, Country:'Switzerland', price:7.509, Location:'Bern'}];
const output = (arr) => arr.map(Object.values);

console.log(output(body));

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.