1

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

8 Answers 8

5

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
Sign up to request clarification or add additional context in comments.

5 Comments

It does return separate arrays for each line as you can see in console jsfiddle.net/Lg0wyt9u/1062
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
sorry my bad I was testing on the wrong data it works fine thank you for your help!
I was wondering if there is way I can get them in sorted order lets say by month then by year?
Sure there is, but maybe you should ask that as another question.
2

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

Comments

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;}
answered Jul 21, 2016 at 12:26

Comments

0

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

Comments

0

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

Comments

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

answered Jul 21, 2016 at 12:40

Comments

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)
});
})
answered Jul 21, 2016 at 12:31

Comments

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);
answered Jul 21, 2016 at 12:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.