[英]How to format data to the given format
Currently I am passing Json data as目前我将 Json 数据作为
coords: [
{lat: 27.17841526682381, lng: 73.29395468749999},
{lat: 24.88842099751237, lng: 73.64551718749999}
]
Instead of this, I need to send Json data as取而代之的是,我需要将 Json 数据发送为
coords: [
{27.17841526682381, 73.29395468749999},
{24.88842099751237, 73.64551718749999}]
How can I able to achieve the same.我怎样才能达到同样的目标。 How can I obtain these result.我怎样才能获得这些结果。
All you need is a very simple map()
operation您只需要一个非常简单的map()
操作
const coords = [ {lat: 27.17841526682381, lng: 73.29395468749999}, {lat: 24.88842099751237, lng: 73.64551718749999} ] const res = coords.map(o => [o.lat, o.lng]) console.log(res)
The easiest way is to use Array#map()
with Object.values()
as a callback:最简单的方法是使用Array#map()
和Object.values()
作为回调:
coords = coords.map(Object.values);
Note:笔记:
Note that your desired output format is incorrect because the inner objects
aren't valid objects
, they should be arrays
.请注意,您想要的输出格式不正确,因为内部objects
不是有效objects
,它们应该是arrays
。
Demo:演示:
let coords = [ {lat: 27.17841526682381, lng: 73.29395468749999}, {lat: 24.88842099751237, lng: 73.64551718749999} ]; coords = coords.map(Object.values); console.log(coords);
Try this.... use Object.values()
试试这个....使用Object.values()
var coords= [ {lat: 27.17841526682381, lng: 73.29395468749999}, {lat: 24.88842099751237, lng: 73.64551718749999} ] let items = []; for (var prop in coords) { var val= Object.values(coords[prop]); items.push(val) } console.log(items)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Assuming that your output should be a nested array like:假设您的输出应该是一个嵌套数组,如:
[[27.17841526682381,73.29395468749999],
[24.88842099751237,73.64551718749999]]
You can use reduce
on your array of objects to achieve this in the following way:您可以通过以下方式在对象数组上使用reduce
来实现此目的:
var coords = [ {lat: 27.17841526682381, lng: 73.29395468749999}, {lat: 24.88842099751237, lng: 73.64551718749999} ]; var formattedCoords = coords.reduce((dict, item) => [...dict, [item.lat, item.lng]], []); console.log(formattedCoords);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.