-1

I create code like this:

for (var b = 0; b < parseDataArr.length; b++) {
    for (var c = 0; c < parseDataArr[b].data_item.length; c++) {
        for (var d = 0; d < parseDataArr[b].data_item[c].data_receipt.length; d++) {
            var kartonReceipt = parseDataArr[b].data_item[c].data_receipt[d].cartonIRBar
        }
    }
}
log.debug('carton for',cartonIRBar) // not all result for loop only last data has record

How do I get all the result data from cartonIRBAr without moving the debug() statement inside the loop?

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
antechf11
  • 55
  • 4
  • 1
    Don't know what's unclear about this question @Daedalus et al. – Robby Cornelissen Feb 24 '23 at 09:08
  • @RobbyCornelissen It wasn't clear what problems the OP was having from their question's earlier wording. That said, I don't see why this question should be reopened at present, unless it is just to close it as a duplicate of another question, like: "[What is the scope of variables in JavaScript?](https://stackoverflow.com/q/500431/785241)" – Daedalus Feb 24 '23 at 09:26
  • @Daedalus OP is not looking for an explanation about why their current approach doesn't work. They're looking for a solution to their problem. There might very well be a duplicate about collecting all values from deeply nested arrays, but I haven't found it yet. – Robby Cornelissen Feb 24 '23 at 09:28
  • 2
    @RobbyCornelissen The other bit of info lacking clarity is if they meant to debug the value of `cartonIRBar`, which isn't defined anywhere in the code, or if it is just a typo and meant to be `kartonReceipt` instead. – Daedalus Feb 24 '23 at 09:34
  • @Daedalus That's a valid point, but we could at least give the question a fighting chance by asking for clarification. – Robby Cornelissen Feb 24 '23 at 09:38
  • @RobbyCornelissen Which is exactly what question closures are supposed to do, that way people don't answer questions with incomplete information. – Daedalus Feb 24 '23 at 09:39
  • @Daedalus Again, a fair point, but if there's *specific* information that we're lacking, it might be helpful to ask for that in a comment, in addition to voting to close? – Robby Cornelissen Feb 24 '23 at 09:43
  • @RobbyCornelissen Also a fair point, even if at the same time it doesn't always scale. However, I'm not about to get into a close war even if I can; [this comment](https://stackoverflow.com/q/75554479/785241?noredirect=1#comment133300926_75554479) contains the details needing clarification; it is now up to the OP to clarify, and to the answerers to wait until that clarification is made(hopefully). – Daedalus Feb 24 '23 at 09:49
  • @RobbyCornelissen Please excuse my lack of timely responses; I seem to be experiencing some amount of lag on the site that I cannot replicate elsewhere. – Daedalus Feb 24 '23 at 09:51
  • @antechf11 inside those nested loops you are bound to get multiple values assigned to `kartonReceipt`, overwriting any previous value. What value do you want to log for `cartonIRBar`? – Hans Kesting Feb 24 '23 at 10:15

1 Answers1

1

To get information about all the cartonIRBar values in that deeply nested array, you need to accumulate those values into a data structure, like an array.

You could use a nested flatMap() approach:

const kartonReceipts = parseDataArr.flatMap(
  ({data_item}) => data_item.flatMap(
    ({data_receipt}) => data_receipt.flatMap(
      ({cartonIRBar}) => cartonIRBar
    )
  )
);

Complete snippet:

const parseDataArr = [{
  data_item: [{
    data_receipt: [{
      cartonIRBar: 'value1'
    }]
  }]
}, {
  data_item: [{
    data_receipt: [{
      cartonIRBar: 'value2'
    }, {
      cartonIRBar: 'value3'
    }]
  }]
}];

const kartonReceipts = parseDataArr.flatMap(
  ({data_item}) => data_item.flatMap(
    ({data_receipt}) => data_receipt.flatMap(
      ({cartonIRBar}) => cartonIRBar
    )
  )
);

console.log(kartonReceipts);
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156