0

enter image description here I am listning to user inputs for a filter value and storing them in to an array. filters = [] my json data is to be filtered based on the values of the filters array.*But the filters can sometimes be empty if users haven't piked certain filters.

filters = ["9999", "91", "2", "5920"]
array = [
        {carID: 38871, carNumber: 5918, dispatchStatus: 2, dataVersionNr: "AAAAAAhqD2Y=", postingID: 91,peratingCompanyID:2180
        },{carID: 38872, carNumber: 5919, dispatchStatus: 2, dataVersionNr: "AAAAAAhqD2Y=", postingID: 81,peratingCompanyID:9999
        },{carID: 38873, carNumber: 5920, dispatchStatus: 2, dataVersionNr: "AAAAAAhqD2Y=", postingID: 91,peratingCompanyID:9999
        }]

I want to check if there are values in the filters and then apply all the filters to the json with this code that I got from the link below

var filtered = array.filter(o => {
    if (filters[0] && o.peratingCompanyID== filters[0]) {
      console.log(filters);
      return true;
    }
    if (filters[1] && o.postingID == filters[1]) {
      return false;
    }
    if (filters[2] && o.dispatchStatus== filters[2]) {
      return false;
    }
    if (filters[3] && o.carNumber == filters[3]) {
      return false;
    }
    return true;
  });

I want to return the json who satisfies the above filter which is

[{carID: 38873, carNumber: 5920, dispatchStatus: 2, dataVersionNr: "AAAAAAhqD2Y=", postingID: 91,peratingCompanyID:9999}]

What am I missing here?

link: Search for multiple filters in JSON using Javascript forEach and indexOf

Second example used

SANA
  • 669
  • 7
  • 12

2 Answers2

2

I suggest to use an object for filtering with the wanted keys and values with the same data type as the data in the objects for filtering.

filters = {
    operatingCompanyID: 9999,
    postingID: 91,
    dispatchStatus: 2,
    carNumber: 5920
}

The result is a new array without items which have some of the filter values.

The filtering with Array#every works like an logical AND, where all items have to match.

If only one item has to match, then you could use Array#some which works like a logical OR.

var filters = filters = { operatingCompanyID: 9999, postingID: 91, dispatchStatus: 2, carNumber: 5920 },
    array = [{ carID: 38871, carNumber: 5918, dispatchStatus: 2, dataVersionNr: "AAAAAAhqD2Y=", postingID: 91, operatingCompanyID: 2180 }, { carID: 38872, carNumber: 5919, dispatchStatus: 2, dataVersionNr: "AAAAAAhqD2Y=", postingID: 81, operatingCompanyID: 9999 }, { carID: 38873, carNumber: 5920, dispatchStatus: 2, dataVersionNr: "AAAAAAhqD2Y=", postingID: 91, operatingCompanyID: 9999 }],
    filtered = array.filter(o => Object
        .entries(filters)
        .every(([key, value]) => o[key] === value)
    );

console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I tried this but it always returns empty array weather any filter is picked or not. – SANA May 18 '18 at 08:41
  • maybe the keys of the filter and in the objects do not match or the type of the items do not match. please have a look to spelling and the case of the keys/strings. – Nina Scholz May 18 '18 at 08:42
  • Checked and re done everything but still the same empty array is being returned – SANA May 18 '18 at 08:49
  • you could check if you take only one filter item and proceed until all items are in the filter object. maybe somthing is not equal in the filter or in the data. – Nina Scholz May 18 '18 at 08:52
  • do you mind to puth the data into the question or at a fiddle to have a look? – Nina Scholz May 18 '18 at 10:41
  • Just added an image id the array from the api,this is the array I wanted to apply the filter. – SANA May 18 '18 at 11:07
  • do you have some asynchron data processing? – Nina Scholz May 18 '18 at 11:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171320/discussion-between-nina-scholz-and-sana). – Nina Scholz May 18 '18 at 11:16
0

I have managed solving this using promise as below

var filters = {
  operatingCompanyID: "",
  postingID: "",
  carNumber: "",
  carAndDriverAttributes: ""
};

the array to be filtered being the var array

 function getposting() {
    var resultposting;
    if (filters.postingID) {
      resultposting = array.filter(function(o) {
        return o.postingID === filters.postingID;
      });
    } else {
      resultposting = array;
    }

    return new Promise(function(res, rej) {
      res(resultposting);
    });
  }

  getposting()
.then(function(result) {
  var resultOcId;
  if (filters.operatingCompanyID) {
    resultOcId = result.filter(function(o) {
      return o.operatingCompanyID === filters.operatingCompanyID;
    });
  } else {
    resultOcId = result;
  }

  return resultOcId;
})
.then(function(result) {
  var resultCarNum;
  if (filters.carNumber) {
    resultCarNum = result.filter(function(o) {
      return o.carNumber === filters.carNumber;
    });
  } else {
    resultCarNum = result;
  }

  return resultCarNum;
})
.then(function(result) {
  var resultAttribute;
  if (filters.carAndDriverAttributes) {
    resultAttribute = result.filter(function(o) {
      if (o.carAndDriverAttributes !== null) {
        return (
          // o.carAndDriverAttributes.indexOf(filters.carAndDriverAttributes) >
          // -1
          o.carAndDriverAttributes === filters.carAndDriverAttributes
        );
      }
    });
  } else {
    resultAttribute = result;
  }

  return resultAttribute;
})
.then(function(result) {
  var resultP;
  if (filters.postingID) {
    resultP = result.filter(function(o) {
      return o.postingID === filters.postingID;
    });
  } else {
    resultP = result;
  }

  return resultP;
})
.then(function(result) {
  updateStatus(result, 0, "freecars");
});
};
SANA
  • 669
  • 7
  • 12