How to access specific items in array? [nodejs]

Hey, guys!
I’m having trouble getting specific items in this array.

[
   {
      "averages":[
         {
            "STATUS":"0",
            "TYPE":"AAA",
         },
         {
            "STATUS":"0",
            "TYPE":"ABC",
         },
         {
            "STATUS":"1",
            "TYPE":"ESD",
         },
         {
            "STATUS":"1",
            "TYPE":"AAA",
         },
      ]
   },
   {
      "averages":[
         {
            "STATUS":"0",
            "TYPE":"CCC",
         },
         {
            "STATUS":"0",
            "TYPE":"AAA",
         },
         {
            "STATUS":"1",
            "TYPE":"ESD",
         },
         {
            "STATUS":"1",
            "TYPE":"XXX",
         },
      ]
   },
   {
      "averages":[
         {
            "STATUS":"1",
            "TIPO":"XXX",
         },
         {
            "STATUS":"1",
            "TYPE":"LLL",
         },
         {
            "STATUS":"1",
            "TYPE":"AAA",
         },
         {
            "STATUS":"1",
            "TYPE":"NU",
         },
         {
            "STATUS":"0",
            "TYPE":"XXX",
         },
         {
            "STATUS":"1",
            "TYPE":"AAA",
         }
      ]
   },
]

I’m trying to separate status from type AAA

[0, 1, 0, 1, 1, 1]

I get the array like this:

	var quality = solicitacoes[0].produtor.dados[0].apiLaticinio[0].qualidade[0]
	var qualityjson = JSON.parse(quality)
	console.log(JSON.stringify(qualityjson));

What nodejs resource can I access the items in the way I mentioned?

Thanks if anyone can help me!

Welcome @Edrian_Biagi,

The question as I understand it may be better at StackExchange.

But I’ll give some ideas.

  • You can do it in the initial query to MongDB instead of sending a huge amount of data. But for that you need to include more information on what db call you are doing.

In your Nodejs code, you can iterate in many different ways:

For iterating an array you can:

  • use while and a counter
  • use Array.forEach
  • use standard for loops
  • use for … in
  • use map (maybe filter in this case)

For example, if quality is the array you start working with, then:

var quality = solicitacoes[0]
  .produtor
  .dados[0]
  .apiLaticinio[0]
  .qualidade[0];


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

const results = getStatus(quality)

Returns:

> getStatus(quality)
[ '0', '1', '0', '1', '1' ]
2 Likes

Thanks for the feedback. Thanks for the mentioned tips. I took the test but it came up:
arr.forEach is not a function

I also created the link in Stack: https://stackoverflow.com/questions/70809595/how-to-access-specific-items-in-array-nodejs

It means the argument is not an Array. To test, use:

const answer = Array.isArray(quality)
console.log('Is quality an array? ', answer)

I dont think you need any of the JSON lines, just pass quality.

Turn this:

	var quality = solicitacoes[0].produtor.dados[0].apiLaticinio[0].qualidade[0]
	var qualityjson = JSON.parse(quality)
	console.log(JSON.stringify(qualityjson));

Into something like this (for debugging):

var quality = solicitacoes[0].produtor.dados[0].apiLaticinio[0].qualidade[0]
if(Array.isArray(quality)) {console.log(getStatus(quality))} 
else { console.log('error. type of quality is: ', typeof(quality))}
1 Like

Thanks, that was exactly the problem. “quality” is not an array.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.