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

So that my final results looks like


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?


It would seem that a simple optimization would be to do it in a single step, i.e. something like:

var finalPassArray = [];
for(var key in _self.productList)
    finalPassArray.push(_self.turnoverMonthlyData.filter(function (el) {
        return el.product == _self.productList[key].product;
    }).map( function(el) {{ el.ordered } ) )

But I would be tempted to use something like reduce:

var results = _self.turnoverMonthlyData.reduce( function(list, el) {
                if (!list[el.product]) list[el.product] = [];
                return list; 
              }, {} );

Which I tweaked slightly to give results as:

{ T0000001_: [48,264,24,67,54],
  T0000002_ :[4567,511,24,18,5],
  T0000003_: [4,57,6,11,544], 

which might be easier to work with.


protected by Simon Forsberg Jun 4 '17 at 0:12

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.