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?
Rory McCrossan
338k41 gold badges322 silver badges353 bronze badges
asked Jul 21, 2016 at 12:19
Voice Of The Rain
5981 gold badge12 silver badges31 bronze badges
8 Answers 8
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);
answered Jul 21, 2016 at 12:24
Nenad Vracar
122k16 gold badges160 silver badges184 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Nenad Vracar
It does return separate arrays for each line as you can see in console jsfiddle.net/Lg0wyt9u/1062
Voice Of The Rain
It returns 2 arrays the first array is always 0 length and the second has 4 elements , I need the first array must have one element and the second must have 3
Voice Of The Rain
sorry my bad I was testing on the wrong data it works fine thank you for your help!
Voice Of The Rain
I was wondering if there is way I can get them in sorted order lets say by month then by year?
Nenad Vracar
Sure there is, but maybe you should ask that as another question.
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);
answered Jul 21, 2016 at 12:25
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Comments
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;}
Comments
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);
answered Jul 21, 2016 at 12:31
Joey Etamity
8664 silver badges9 bronze badges
Comments
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)
answered Jul 21, 2016 at 12:37
raphaëλ
6,5412 gold badges31 silver badges35 bronze badges
Comments
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);
answered Jul 21, 2016 at 12:40
Marcus Vilete
2204 silver badges8 bronze badges
Comments
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)
});
})
answered Jul 21, 2016 at 12:31
black_pottery_beauty
8798 silver badges17 bronze badges
Comments
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);
Comments
lang-js