我有以下对象数组:
[{
"Lines": [{
"Month": 10,
"Year": 2017,
"CompletionPercentage": 30
}]
}, {
"Lines": [{
"Month": 10,
"Year": 2017,
"CompletionPercentage": 30
}, {
"Month": 6,
"Year": 2017,
"CompletionPercentage": 30
}, {
"Month": 12,
"Year": 2017,
"CompletionPercentage": 40
}]
}]
我需要将每个line
属性转换为单独的javascript数组
第1行必须是
[
["10-2017", 30]
]
第2行必须
[
["10-2017", 30],
["6-2017", 30],
["12-2017", 30]
]
我怎样才能做到这一点?
答案 0 :(得分:5)
您可以使用map()
来获得此结果。
var data = [{"Lines":[{"Month":10,"Year":2017,"CompletionPercentage":30}]},{"Lines":[{"Month":10,"Year":2017,"CompletionPercentage":30},{"Month":6,"Year":2017,"CompletionPercentage":30},{"Month":12,"Year":2017,"CompletionPercentage":40}]}]
var result = data.map(function(e) {
return e.Lines.map(function(a) {
return [a.Month + '-' + a.Year, a.CompletionPercentage];
})
})
console.log(result);
答案 1 :(得分:2)
您可以使用嵌套的Array#map
并在数组中返回all。
var array = [{ "Lines": [{ "Month": 10, "Year": 2017, "CompletionPercentage": 30 }] }, { "Lines": [{ "Month": 10, "Year": 2017, "CompletionPercentage": 30 }, { "Month": 6, "Year": 2017, "CompletionPercentage": 30 }, { "Month": 12, "Year": 2017, "CompletionPercentage": 40 }] }],
result = array.map(function (a) {
return a.Lines.map(function (b) {
return [b.Month + '-' + b.Year, b.CompletionPercentage];
});
});
console.log(result);
答案 2 :(得分:0)
function objToArray($obj, &$arr){
if(!is_object($obj) && !is_array($obj)){
$arr = $obj;
return $arr;
}
foreach ($obj as $key => $value)
{
if (!empty($value))
{
$arr[$key] = array();
objToArray($value, $arr[$key]);
}
else
{
$arr[$key] = $value;
}
}
return $arr;}
答案 3 :(得分:0)
请检查https://jsfiddle.net/1qx6xqv4/
您可以使用嵌套Array.map(function(data){})
执行此操作:
var data = [{"Lines":[{"Month":10,"Year":2017,"CompletionPercentage":30}]}, {"Lines":[{"Month":10,"Year":2017,"CompletionPercentage":30}, {"Month":6,"Year":2017,"CompletionPercentage":30}, {"Month":12,"Year":2017,"CompletionPercentage":40}]}];
var array = data.map(function (ls){
return ls.Lines.map(function(line){
return [line.Month + '-' + line.Year, line.CompletionPercentage];
})
})
console.log(array);
答案 4 :(得分:0)
var abcd = [{"Lines":[{"Month":10,"Year":2017,"CompletionPercentage":30}]},{"Lines":[{"Month":10,"Year":2017,"CompletionPercentage":30},{"Month":6,"Year":2017,"CompletionPercentage":30},{"Month":12,"Year":2017,"CompletionPercentage":40}]}]
var array = [];
var secondArray = [];
var innerArray = [];
$.each(abcd,function(i,data){
secondArray = [];
$.each(data,function(j,lineData){
$.each(lineData,function(k,innerData){
innerArray = [];
innerArray.push(innerData.Month+'-'+innerData.Year,innerData.CompletionPercentage);
secondArray.push(innerArray)
})
array.push(secondArray)
});
})
答案 5 :(得分:0)
ES6答案
const a = [{
"Lines": [
{"Month": 10, "Year": 2017, "CompletionPercentage": 30}]
}, {
"Lines": [{
"Month": 10,
"Year": 2017,
"CompletionPercentage": 30
}, {"Month": 6, "Year": 2017, "CompletionPercentage": 30}, {"Month": 12, "Year": 2017, "CompletionPercentage": 40}]
}]
const r = a.map(e => e["Lines"]).map(e => e.map(e2=>[`${e2.Month}-${e2.Year}`,e2.CompletionPercentage]))
console.log(r)
答案 6 :(得分:0)
var lines = [{ "Lines": [{ "Month": 10, "Year": 2017, "CompletionPercentage": 30 }] }, { "Lines": [{ "Month": 10, "Year": 2017, "CompletionPercentage": 30 }, { "Month": 6, "Year": 2017, "CompletionPercentage": 30 }, { "Month": 12, "Year": 2017, "CompletionPercentage": 40 }] }];
var result = [];
for (i = 0; i < lines.length; i++) {
for (j = 0; j < lines[i].Lines.length; j++) {
result.push([lines[i].Lines[j].Month + '-' + lines[i].Lines[j].Year, lines[i].Lines[j].CompletionPercentage]);
}
}
console.log(result);
答案 7 :(得分:0)
var object = [{ "Lines": [{ "Month": 10, "Year": 2017, "CompletionPercentage": 30 }] }, { "Lines": [{ "Month": 10, "Year": 2017, "CompletionPercentage": 30 }, { "Month": 6, "Year": 2017, "CompletionPercentage": 30 }, { "Month": 12, "Year": 2017, "CompletionPercentage": 40 }] }];
var outerArray = []
for(i=0;i<object.length;i++){
var objs = object[i].Lines;
var innerArr = [];
for (j=0;j<objs.length;j++){
var innerObj = objs[j];
var month = innerObj.Month;
var year = innerObj.Year;
var CompletionPercentage = innerObj.CompletionPercentage;
innerArr.push([month+"-"+year,CompletionPercentage]);
}
outerArray.push(innerArr);
}
console.log(outerArray);
JSON.stringify(outerArray);