我有一个以特定格式显示的数组,我需要对其进行修改,以便以其他格式显示.
这是代码:
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 ];
Run Code Online (Sandbox Code Playgroud)
这显示如下数据:
["first place,50", "second place,50", "third place,50"]
Run Code Online (Sandbox Code Playgroud)
但我需要将数据显示如下:
[ ['first place', 50],['second place', 50],['third place', 50] ];
Run Code Online (Sandbox Code Playgroud)
如何更改代码以便以所需格式显示?
将元素作为数组返回到Array#map方法回调中.
var phases = mainData.places.map(function(e){
return [e.name , 50];
});
Run Code Online (Sandbox Code Playgroud)
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);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40 次 |
| 最近记录: |