'Nested for loop through json object array does not work

I need to build a new array based on json array (Context) bellow. Unfortunately I never reach the outer Loop after passing by first run. Is there any mistake in code? How can I solve this issue?

Thank you for help.

Context:

"rfqBp": [
    {
      "rfqBpId": 1041650,
      "Contact": [
        {
          "ID": 1000014,
          "SelectedContact": true
        },
        {
          "ID": 1002411,
          "SelectedContact": true
        },
        {
          "ID": 1016727,
          "SelectedContact": true
        },
        {
          "ID": 1017452,
          "SelectedContact": true
        }
      ],
    },
    {
      "rfqBpId": 1052326,
      "Contact": [
        {
          "ID": 1016236,
          "SelectedContact": true
        },
        {
          "ID": 1019563,
          "SelectedContact": true
        }
      ],
    },
    {
      "rfqBpId": 1056632,
      "Contact": [
        {
          "ID": -1,
          "SelectedContact": false
        }
      ],
    },
    {
      "rfqBpId": 1056637,
      "Contact": [
        {
          "ID": 1019875,
          "SelectedContact": true
        }
      ],
    }
  ],

script:

$scope.SelectedContacts = function() { //function starts by click on checkbox in html
  let selectedContactList = [];
  let finalList = [];
  $scope.Context.Output = [];
  for (let i = 0; i <= $scope.Context.rfqBp.length; i++) {
    for (let j = 0; j <= $scope.Context.rfqBp[i].Contact.length; j++) {
      if ($scope.Context.rfqBp[i].Contact[j].SelectedContact === true) {
        selectedContactList = {
          "ID": $scope.Context.rfqBp[i].Contact[j].ID
        };
        finalList.push(selectedContactList);

      } else if ($scope.Context.rfqBp[i].Contact[j].SelectedContact !== true) {
        continue;
      }
      $scope.Context.Output = finalList; //Output works but just for rfqBp[1]
    };
  };
  $scope.Context.Output = finalList; //this part never reached
};

Output:

  "Output": [
    {
      "ID": 1000014
    },
    {
      "ID": 1016727
    },
    {
      "ID": 1017452
    }
  ]

I try to get following:

  "Output": [
    {
      "ID": 1000014
    },
    {
      "ID": 1016727
    },
    {
      "ID": 1017452
    },
    {
      "ID": 1016236
    },
    {
      "ID": 1019563
    },
    {
      "ID": 1019875
    }
  ]


Solution 1:[1]

You can use Array.prototype.flatMap() combined with Array.prototype.filter(), Array.prototype.map() and Destructuring assignment:

const rfqBp = [{rfqBpId: 1041650,Contact: [{ID: 1000014,SelectedContact: true,},{ID: 1002411,SelectedContact: true,},{ID: 1016727,SelectedContact: true,},{ID: 1017452,SelectedContact: true,},],},{rfqBpId: 1052326,Contact: [{ID: 1016236,SelectedContact: true,},{ID: 1019563,SelectedContact: true,},],},{rfqBpId: 1056632,Contact: [{ID: -1,SelectedContact: false,},],},{rfqBpId: 1056637,Contact: [{ID: 1019875,SelectedContact: true,},],},]

const result = rfqBp
  .flatMap(({ Contact }) => Contact
    .filter(({ ID }) => ID > 0) // Filter to exclude negative `ID`s
    .map(({ ID }) => ({ ID }))
  )

console.log(result)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1