gpt4 book ai didi

Javascript 更改数组以查看特定格式

转载 作者:行者123 更新时间:2023-11-30 07:34:21 25 4
gpt4 key购买 nike

我有一个以特定格式显示的数组,我需要对其进行修改以使其以另一种格式显示。

代码如下:

var arr = {
"id":"100",
"name":"the name",
"places":[
{
"id":"23",
"name":"first place",
"startDate":"2015-01-30 15:01:00",
"endDate":"2015-01-30 17:01:00"
},

{
"id":"54",
"name":"second place",
"startDate":"2015-01-31 17:01:00",
"endDate":"2015-02-01 17:01:00"

},
{
"id":"400",
"name":"third place",
"startDate":"2015-02-01 17:01:00",
"endDate":"2015-02-05 17:01:00"

}

]

}

var phases = mainData.places.map(function(e){
return e.name + "," + 50;
});

myvar = [ places ];

这是显示这样的数据:

["first place,50", "second place,50", "third place,50"]

但是我需要这样显示数据:

[ ['first place', 50],['second place', 50],['third place', 50] ];

如何更改代码以使其以所需格式显示?

最佳答案

将元素作为数组返回到 Array#map 方法回调。

var phases = mainData.places.map(function(e){
return [e.name , 50];
});

var arr = {
"id": "100",
"name": "the name",
"places": [{
"id": "23",
"name": "first place",
"startDate": "2015-01-30 15:01:00",
"endDate": "2015-01-30 17:01:00"
}, {
"id": "54",
"name": "second place",
"startDate": "2015-01-31 17:01:00",
"endDate": "2015-02-01 17:01:00"

}, {
"id": "400",
"name": "third place",
"startDate": "2015-02-01 17:01:00",
"endDate": "2015-02-05 17:01:00"
}]
}

var phases = arr.places.map(function(e) {
return [e.name, 50];
});

console.log(phases);

关于Javascript 更改数组以查看特定格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38657874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com