简体   繁体   English

reactJS从数组中删除项目

[英]reactJS remove Items from an array

I fetch a list of users from aws cognitio, that works perfectly. 我从aws cognitio中获取用户列表,这非常有效。

Now I want to iterate over this array and remove those which don't match to a Client ID, that does not work properly. 现在我想迭代这个数组并删除那些与客户机ID不匹配的数据,这些数据无法正常工作。

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. 在这种情况下,我有两个用户,其中clientID = 36,另一个客户端ID = 35。

only the one with 36 should be displayed 只显示36的那个

在此输入图像描述

Question: Should I do this recoursive? 问题:我应该这样做吗? Breac the foreach when one is found and start again? 当一个人被发现并重新开始时,Breac就会出现这个问题? maybe of wrong indexing? 可能是错误的索引?

what is my failure in this case? 在这种情况下我的失败是什么?

You are mutating array while iterating over it. 你在迭代它时改变数组。 Use filter instead. 请改用filter

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'))) ) 

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)),
    });
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM