1

I fetch a list of users from aws cognitio, that works perfectly.

Now I want to iterate over this array and remove those which don't match to a Client ID, that does not work properly.

what is my failure in this case?

My code looks like this:

this.awsSDKAuth().listUsers(params, (err, data) => {
    if (err) console.log(err, err.stack); // an error occurred
    else {
        let userArray = data.Users.slice(0);

        console.log(userArray);
        userArray.forEach((user,index) => {
            user.Attributes.forEach(attr =>  {
                if(attr.Name === "custom:client" && attr.Value !== clientId){
                        userArray.splice(index,1);
                    console.log(userArray);
                }
            }
        )});
        console.log(userArray);
        this.setState({
            users: userArray
        })
    }
});

Thanks

In this case I got two useres one with clientID = 36 and one with clientID = 35.

only the one with 36 should be displayed

enter image description here

Question: Should I do this recoursive? Breac the foreach when one is found and start again? maybe of wrong indexing?

5
  • your if else statement looks like it has a weird syntax. If { } else { }. If that's fine for you then probably should show us what is the error message? Commented Sep 11, 2017 at 5:28
  • that weired if else comes from aws ;) just copy paste, but you're right I'll fix it. Problem is that not all users that matches the second if are removed Commented Sep 11, 2017 at 5:30
  • What does each user object look like ? Commented Sep 11, 2017 at 5:31
  • added some more info above Commented Sep 11, 2017 at 5:34
  • @Felix...if you change your existing code with attr.Value !== clientId to attr.Value === clientId. Does your code works? Commented Sep 11, 2017 at 6:14

2 Answers 2

3

what is my failure in this case?

You are mutating array while iterating over it. Use filter instead.

this.awsSDKAuth().listUsers(params, (err, data) => {
    if (err) console.log(err, err.stack); // an error occurred
    else {
        let userArray = data.Users.slice(0)
           .filter(user => user.Attributes.some(attr => attr.Name === "custom:client" && attr.Value === clientId));

        this.setState({
            users: userArray
        })
    }
});

const userData = [{
    Attributes: [{
        name: 'custom:client',
        value: '36'
      },
      {
        name: 'FirstName',
        value: 'Keep'
      }
    ]
  },
  {
    Attributes: [{
        name: 'custom:client',
        value: '35'
      },
      {
        name: 'FirstName',
        value: 'Omit'
      }
    ]
  },
  {
    Attributes: [{
        name: 'custom:client',
        value: '36'
      },
      {
        name: 'FirstName',
        value: 'Keep'
      }
    ]
  },
  {
    Attributes: [{
        name: 'custom:client',
        value: '37'
      },
      {
        name: 'FirstName',
        value: 'Omit'
      }
    ]
  }
]

console.log(
  userData.filter(user => user.Attributes.some(attr => (attr.name === 'custom:client' && attr.value === '36')))
)

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

5 Comments

does not work as I thought about, I get the one with ide 36 and lots of others with 35
shouldn't the condition be attr.Value === clientId
@Felix Right you need attr.Value === clientId
@YuryTarabanko.... I want to understand how array mutates if we use foreach over it. Could you explain a little bit?
@MukulSharma It is not forEach but splice mutates array in place. When you remove an item all indices shift.
0

This might do it for you. You can filter the users based on the criteria that the user returns an attribute that has the correct name and value.

this.awsSDKAuth().listUsers(params, (err, data) => {
  if (err) {
    console.log(err, err.stack); // an error occurred
  } else {
    this.setState({
      users: data.Users.filter(user => user.Attributes.some(attr => attr.Name === "custom:client" && attr.Value === clientID)),
    });
  }
});

3 Comments

looks nice but now I got more wrong users in the array... lots of the users with client ID 35 ... only want those with 36
Updated the answer
does not work. clientID is an string (comes from aws) and I want to use clientID as a var.

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.