简体   繁体   中英

how convert array to object with specific key in javascript

I have a big array with below format

 [
    { 
      id: '1', 
      name: "bloh", 
      coordinates: [[51.3888562, 35.7474398], [51.388671,35.7470575],[51.3887346, 35.7470375]]
    },
    {
      id: '2', 
      name: "blohbloh",
      coordinates:  [[51.3888562, 35.7474398],[51.3822271, 35.7444575]]
    }
 ]

I want to convert format of coordinates property of all elements to below format

{
   id: '1',
   name: "bloh",
   coordinates:[{longitude:51.3888562,latitude: 35.7474398},{longitude:51.3887346,latitude: 35.7470375}]
},

{
   id: '2', 
   name: "blohbloh", 
   coordinates:[{longitude:51.3888562,latitude: 35.7474398},{longitude:51.3822271,latitude: 35.7444575}]
}

You should be able to do this with map:

 let input = [{id: '1', name: "bloh", coordinates: [[51.3888562, 35.7474398], [51.388671,35.7470575],[51.3887346, 35.7470375]]},{id: '2', name: "blohbloh",coordinates: [[51.3888562, 35.7474398],[51.3822271, 35.7444575]]}]; let result = input.map(({id, name, coordinates}) => { return { id, name, coordinates: coordinates.map(([latitude,longitude]) => { return { latitude, longitude}})}; }); console.log("Result: ", result); 

You can loop through the array and then you need to use map to change representation of coordinates from array to object.

You can use following code to convert.

var arr=[{id: '1', name: "bloh", coordinates:  [[51.3888562, 35.7474398], [51.388671,35.7470575],[51.3887346, 35.7470375]]},{id: '2', name: "blohbloh",coordinates: [ [51.3888562, 35.7474398],[51.3822271, 35.7444575]]}];
arr.forEach(elm=>elm.coordinates=elm.coordinates.map(c=>({longitude:c[0],latitude:c[1]})));
console.log(arr);

You can use two map() and destructuring to copy all properties of the top-level objects in a rest variable and map() the coordinates to another format:

 const data = [{id: '1', name: "bloh", coordinates:[[51.3888562, 35.7474398], [51.388671,35.7470575],[51.3887346, 35.7470375]]},{id: '2', name: "blohbloh",coordinates:[[51.3888562, 35.7474398],[51.3822271, 35.7444575]]}]; const result = data.map(({ coordinates, ...rest }) => ({ ...rest, coordinates: coordinates.map(([longitude, latitude]) => ({ longitude, latitude })) }) ); console.log(result); 

In any kind of problem you face, first, you should understand what you need to do, and prettier data will always help you to achieve this.

You want to convert this:

const original = [{
  id: '1',
  name: "bloh",
  coordinates: [ [51.3888562, 35.7474398],
               [51.388671,35.7470575],
               [51.3887346, 35.7470375] ]
 },
 {
  id: '2',
  name: "blohbloh",
  coordinates:  [ [51.3888562, 35.7474398],
                [51.3822271, 35.7444575] ]
 }
]

To this:

{
  id: '1',
  name: "bloh",
  coordinates:[
    {longitude:51.3888562,latitude: 35.7474398},
    {longitude:51.3887346,latitude: 35.7470375}
   ]
},
{
  id: '2',
  name: "blohbloh",
  coordinates:[
    {longitude:51.3888562,latitude: 35.7474398},
    {longitude:51.3822271,latitude: 35.7444575}
   ]
}

We will use the 'for' loop because it is easier to see what's going on, in my own opinion.

for(i in original){
  original[i].coordinates = original[i].coordinates.map(item => {return {
    latitude: item[0], 
    longitude: item[1]}
    })
}

console.log(JSON.stringify(original))

You can use prettifier to see your data in more pretty way,for example using this resource: https://jsonformatter.curiousconcept.com/

Hope this will help.

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