Home > Software design >  Filter Array - if the array exists in the array of objects
Filter Array - if the array exists in the array of objects

Time:11-17

How Do I filter an array by checking if an array is listed in the array?

cryptoData: Array(100) 0: {CoinInfo: {…}, RAW: {…}, DISPLAY: {…}}

I am trying to check if cryptoData (Array) contains the DISPLAY array and if it does not contain DISPLAY - remove it from the cryptoData array and set the filterArray with only the elements that contain the DISPLAY Array.

My attempt:

var filterdArray = cryptoData.filter(function (el) {
  return cryptoData[el] === 'DISPLAY';
});

this.setState({
  cryptos: filterdArray,
  refreshing: false,
});

CodePudding user response:

As the value is an object you can use hasOwnProperty to check if the object has a property.

var filterdArray = cryptoData.filter(function (el) {
  return el.hasOwnProperty('DISPLAY');
});

this.setState({
  cryptos: filterdArray,
  refreshing: false,
});
  • Related