Filtering JSON by object property value

Posted on

Problem

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?

Solution

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] = [];
                list[el.product].push(el.ordered);
                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.

Leave a Reply

Your email address will not be published. Required fields are marked *