I am attempting to filter JSON into an array of arrays containing a property value based on the product. The form is as follows:
[{
"product": "T0013415_",
"ordered": 48,
"shipped": 48,
"year": 2016,
"month": 6
}, {
"product": "T0054113_",
"ordered": 379,
"shipped": 379,
"year": 2016,
"month": 7
},......]
I am first filtering this JSON into an array of arrays of objects seperated by product with:
var firstPassArray = [];
for(var key in _self.productList)
{
firstPassArray.push(_self.turnoverMonthlyData.filter(function (el) {
return el.product == _self.productList[key].product;
}))
}
The result being an array of arrays of objects:
[[Object, Object, ...],[Object, Object, ...],[Object, Object, ...],...]
I am then getting the actual property values into arrays with
var finalPassArray = [];
for (var key2 in firstPassArray)
{
var insideArray = [];
for (var index = 0; index < firstPassArray[key2].length ; index++)
{
insideArray.push(firstPassArray[key2][index].ordered);
}
finalPassArray.push(insideArray);
}
So that my final results looks like
[[48,264,24,67,54],[4567,511,24,18,5],[4,57,6,11,544],[78,98,77,88,77],[454,54,4,6,8]]
Ignoring bad variable names, is there a better way to do this? Like returning a object property value in the filter()
method rather than an object?