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

How to exclude from foreach an array that does not contain an object?

I’m receiving an array and performing a treatment to extract some information.

When I get the matrix like this, everything goes well!

[
  {
    averages: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  },
  {
    averages: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  },
  {
    averages: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ]
  },
  {
    averages: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ]
  }
]

But when the array comes like this:

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

[
  {
    averages: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  },
  {
    averages: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  },
  {
    averages: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  },
  { averages: 'No data in period.' }
]

I get this error:

ccs.medias.forEach is not a function

Could this be the error?

{ averages: ‘No data in period.’ }

The code mentioned to filter the information I want is this:

    function test(arr) {
        let results = [];
        arr.forEach(ccs => {
          ccs.medias.forEach(element => {
            if (element["TYPE"] === 'AAA') {
              results.push(element["STATUS"])
            }
          })
        })
        return results;
    }

As I mentioned above, this code works for the first array example, but not for the following one.
I appreciate if anyone can help me analyze it!

>Solution :

Yep, you guessed right, problem is here:

{ averages: 'No data in period.' }

Before using .forEach(), you need to make sure property value (ccs.averages) is array, so just add simple if-condition like:

if (Array.isArray(ccs.averages)) {
  // Do what you need with array and only
}

Full code:

const arr = [
  {
    averages: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  },
  {
    averages: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  },
  {
    averages: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
    ]
  },
  { averages: 'No data in period.' }
]

function test(arr) {
  let results = [];
  arr.forEach(ccs => {
    // Make sure .avarages is array
    if (Array.isArray(ccs.averages)) {
      ccs.averages.forEach(element => {
      if (element["TYPE"] === 'AAA') {
        results.push(element["STATUS"])
      }
    })
    }
  })
  return results;
}

console.log(test(arr));

Refs: Array.isArray().

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