简体   繁体   中英

Convert array of objects to nested array

I have the following array of objects:

[{
    "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
    }]
}]

I need to convert each line property to a separate javascript array

Line1 must be

[
    ["10-2017", 30]
]

Line2 must be

[
    ["10-2017", 30],
    ["6-2017", 30],
    ["12-2017", 30]
]

How can I achieve that?

You can use map() to get this result.

 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); 

You could use nested Array#map and return all in an array.

 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); 

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;}

Please check https://jsfiddle.net/1qx6xqv4/

You can use nested Array.map(function(data){}) to do this:

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);
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)
});

})

An ES6 answer

 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) 

 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); 

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM