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
    }
  ]

1 answer

  • answered 2022-05-06 09:07 Yosvel Quintero Arguelles

    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)

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum