1

I have this 2 dimensional array =

0: (3) [true, false, false]
1: (3) [true, true, false]
2: (3) [true, true, true]
3: (3) [false, false, false]

The position in the array represents the same in each i.e 0 = "Learner" 1 = "Manager", 2 = "ClientAdmin"

I want a new 2 dimensional array that looks like below

0: (3) ["Learner"]
1: (3) ["Learner", "Manager"]
2: (3) ["Learner", "Manager", "ClientAdmin"]
3: (3) []

I have tried

selectedAudienceMandatoryArrayText = []

this.selectedAudienceMandatoryArray.forEach( (isArray, index) => {
          if (isArray[0] == true) {
            this.selectedAudienceMandatoryArrayText[index].push("Learner");
          }
          if (isArray[1] == true) {
            this.selectedAudienceMandatoryArrayText[index].push("Manager");
          }
          if (isArray[2] == true) {
            this.selectedAudienceMandatoryArrayText[index].push("ClientAdmin"); 
          }
        }

but I get the error: Cannot read property 'push' of undefined

What is the most efficient way to do this. ES6 solutions welcome.

5
  • Sorry it is part of the angular component I am working on. selectedAudienceMandatoryArrayText = []. I'll edit the question Commented Dec 4, 2020 at 17:44
  • 2
    [][0] is undefined, so undefined.push is where your error is coming from Commented Dec 4, 2020 at 17:45
  • Ok. How do push to selectedAudienceMandatoryArrayText create a 2 dimensional array? Otherwise I will get ["Learner", "Learner", "Manager", "Learner", "Manager", "ClientAdmin"] I don't want them all in one array. I need them in a 2 dimensional array. Commented Dec 4, 2020 at 17:48
  • selectedAudienceMandatoryArrayText.push([]) as the first thing in your forEach to make a new element at the same index as the index you are processing on the other array Commented Dec 4, 2020 at 17:49
  • I understand thanks, but I need to change the logic. Can I use an if inside the push? Commented Dec 4, 2020 at 17:51

2 Answers 2

3

You could check if the flag is set, then take the value from roles with the index or return an empty array.

const
    roles = ["Learner", "Manager", "ClientAdmin"],
    data = [[true, false, false], [true, true, false], [true, true, true], [false, false, false]],
    result = data.map(a => a.flatMap((f, i) => f ? roles[i] : []));
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

3 Comments

Works great. Just need to get my head around it. Not seen flatMap before.
have a look here: Array#flatMap
This is just what I was looking for. Thanks!
0
selectedAudienceMandatoryArrayText = [];

this.selectedAudienceMandatoryArray.forEach(isArray => {
  const roles = [];
  
  if (isArray[0]) roles.push('Learner');
  if (isArray[1]) roles.push('Manager');
  if (isArray[2]) roles.push('ClientAdmin');
  
  selectedAudienceMandatoryArrayText.push(roles);
}

You could push to a new array for each loop, and at the end, push that to the other array. This reduces having to keep track of the index for the outer array.

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.