Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter Array – if the array exists in the array of objects

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My attempt:

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

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

>Solution :

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,
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading