2

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
    }
  ]
8
  • The only way you could not get to that statement is if there's an error that stops the loops. Is there any error message in the console? Commented May 6, 2022 at 8:27
  • Hi! Please update your question with a minimal reproducible example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button); here's how to do one. Your various "before" structures start in the middle of something, which makes it very hard to help you. Commented May 6, 2022 at 8:27
  • 2
    JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Commented May 6, 2022 at 8:27
  • you are overwriting $scope.Context.Output = finalList; each iteration Commented May 6, 2022 at 8:28
  • 1
    <= should be <. You're accessing outside the array and getting an error. Commented May 6, 2022 at 8:34

1 Answer 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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.