reactjs React/Javascript将AWS listUser数组转换为扁平数组

tktrz96b  于 2023-04-29  发布在  React
关注(0)|答案(1)|浏览(70)

我正在使用AWS Cognito listUsers API从我的Cognito池中获取所有用户的列表。我已经成功地返回了用户数组,但现在我被困在如何将该数组转换为扁平数组上。以下是我从AWS listUsers API收到的数据示例:

[
        {
            Username: "xx001",
            Attributes: [
                {
                    Name: "given_name",
                    Value: "Jeff"
                },
                {
                    Name: "family_name",
                    Value: "Smith"
                },
                {
                    Name: "email",
                    Value: "jeff.smith@email.com"
                }
            ],
            UserStatus: "Confirmed",
        },
        {
            Username: "yy002",
            Attributes: [
                {
                    Name: "given_name",
                    Value: "Steve"
                },
                {
                    Name: "family_name",
                    Value: "Jones"
                },
                {
                    Name: "email",
                    Value: "steve.jones@email.com"
                }
            ],
            UserStatus: "Confirmed",
        }
    ]

我想把上面的数组转换成下面的结构:

[
    {
        Username: "xx001",
        given_name: "Jeff",
        family_name: "Smith",
        email: "jeff.smith@email.com",
        UserStatus: "Confirmed"
    },
    {
        Username: "yy002",
        given_name: "Steve",
        family_name: "Jones",
        email: "steve.jones@email.com"
        UserStatus: "Confirmed"
    }
]

如果有人能给我指出正确的方向,我将不胜感激!

hl0ma9xz

hl0ma9xz1#

你可以使用map来转换数组

const users = [        {            Username: "xx001",            Attributes: [                {                    Name: "given_name",                    Value: "Jeff"                },                {                    Name: "family_name",                    Value: "Smith"                },                {                    Name: "email",                    Value: "jeff.smith@email.com"                }            ],            UserStatus: "Confirmed",        },        {            Username: "yy002",            Attributes: [                {                    Name: "given_name",                    Value: "Steve"                },                {                    Name: "family_name",                    Value: "Jones"                },                {                    Name: "email",                    Value: "steve.jones@email.com"                }            ],            UserStatus: "Confirmed",        }    ]
    
const res = users.map(({Attributes,...rest}) => {
  const attrObj = Attributes.reduce((acc,{Name,Value}) => ({...acc, [Name]:Value}),{})
  return {...rest,...attrObj }
 })
 
console.log(res)

如果属性是固定的,而不是动态的,你甚至不必使用reduce和spread。因此,这将比第一个更快。

const users = [        {            Username: "xx001",            Attributes: [                {                    Name: "given_name",                    Value: "Jeff"                },                {                    Name: "family_name",                    Value: "Smith"                },                {                    Name: "email",                    Value: "jeff.smith@email.com"                }            ],            UserStatus: "Confirmed",        },        {            Username: "yy002",            Attributes: [                {                    Name: "given_name",                    Value: "Steve"                },                {                    Name: "family_name",                    Value: "Jones"                },                {                    Name: "email",                    Value: "steve.jones@email.com"                }            ],            UserStatus: "Confirmed",        }    ]
    
const res = users.map(({Attributes,Username,UserStatus}) => {  
  return {Username,UserStatus, [Attributes[0]["Name"]]:Attributes[0]["Value"],[Attributes[1]["Name"]]:Attributes[1]["Value"],[Attributes[2]["Name"]]:Attributes[2]["Value"]}
 })
 
console.log(res)

相关问题