简体   繁体   English

如何在JavaScript中使用特定键将数组转换为对象

[英]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: 您应该能够使用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. 您可以遍历数组,然后需要使用map将坐标的表示形式从数组更改为对象。

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: 您可以使用两个map()解构操作,以将rest对象的所有属性复制到rest变量中,然后将map()的坐标转换为另一种格式:

 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”循环,因为它更容易看到正在发生的事情。

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/ 您可以使用修饰符以更漂亮的方式查看数据,例如,使用以下资源: https : //jsonformatter.curiousconcept.com/

Hope this will help. 希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数组转换为具有每个键的特定名称的JavaScript对象 - How to convert an array into a JavaScript object with specific names for each key 如何在JavaScript中将具有属性和键的对象转换为Array? - How to convert an object with property and key to Array in JavaScript? 如何将数组转换为 object 并在此 object Javascript 中添加相同的密钥? - How to convert array to object with add same key in this object Javascript? 如何使用映射的键值对将数组转换为javascript中的对象? - How to convert an array into an object in javascript with mapped key-value pairs? 如何在 JavaScript 中将嵌套对象数组转换为具有键值对的对象 - How to convert an array of nested objects to an object with key, value pairs in JavaScript 如何将特定键值从 object 转换为数组 javascript - How to convert particular key value from object to array javascript 如何将 Object {} 转换为 JavaScript 中键值对的数组 [] - How to convert an Object {} to an Array [] of key-value pairs in JavaScript 如何将JSON对象转换为按键过滤的Javascript数组? - How to convert a JSON object to Javascript array filtered by key? Javascript:如何在我的阵列中使用特定键删除 object? - Javascript: How do I remove an object with a specific key in my array? 如何在 javascript 中的另一个数组 object 中移动特定键值 - How to move specific key value inside another array object in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM