Home > Mobile >  How to access specific items in array? [nodejs]
How to access specific items in array? [nodejs]

Time:01-22

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));

I tried to use this material, but without success: enter link description here

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

Thanks if anyone can help me!

CodePudding user response:

I believe you're saying that you want to turn an array of objects with a STATUS field into an array of numbers. Let's say you have a single item from your above array:

   {
      "averages":[
         {
            "STATUS":"0",
            "TYPE":"AAA",
         },
         {
            "STATUS":"0",
            "TYPE":"ABC",
         },
         {
            "STATUS":"1",
            "TYPE":"ESD",
         },
         {
            "STATUS":"1",
            "TYPE":"AAA",
         },
      ]
   },

Assuming the above is parsed and stored as item:

const statuses = item.averages.map(i => i.STATUS);

Array.prototype.map will let you manipulate each element of an array in the same way and return the new array (which will have the same number of elements as the old one).

In this case, we take an object with a STATUS and TYPE field, and instead return only the value of the STATUS field. So we end up with an array of numbers; [0, 0, 1, 1].

CodePudding user response:

To get all the status from the given array you can just use Array.map() inside Array.map() like below:

const getItems = (array = items) => {
  return array.map((arItem, _) => {
    return arItem.averages.map((nestItem, _) => {
      return nestItem.STATUS
    })
  })
}

console.log([].concat(...getItems()))

//Output:
//[
//  '0', '0', '1', '1',
//  '0', '0', '1', '1',
//  '1', '1', '1', '1',
//  '0', '1'
//]

However if you are trying to get the items with TYPE='AAA', then you can use the following code:

const getTypeAAAItems = (array = items) => {
  return array.map((arItem, _) => {
    return arItem.averages.filter((nestItem) => {
      return nestItem.TYPE === 'AAA'
    })
  })
}

//Output:
//[
//  { STATUS: '0', TYPE: 'AAA' },
//  { STATUS: '1', TYPE: 'AAA' },
//  { STATUS: '0', TYPE: 'AAA' },
//  { STATUS: '1', TYPE: 'AAA' },
//  { STATUS: '1', TYPE: 'AAA' }
//]
  •  Tags:  
  • Related