[英]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.