gpt4 book ai didi

javascript - 如何映射和更改数组内的值?

转载 作者:行者123 更新时间:2023-12-03 00:26:02 25 4
gpt4 key购买 nike

我对 JavaScript 还很陌生,我想知道如何检查 Angular 色类型的每次迭代并更改值,并根据 Angular 色类型相应地更改它。

array = [
{
id: "1",
role: "User"
},
{
id: "2",
role : "Provider"
},
{
id: "3",
role: "Administration"
}
]

// If role: User, change to role: "Disable"
// If role: Provider, change role to "Read Me"
// If role Administration, change role to "Admin"

最佳答案

这样的事情应该有效:

let array = [
{
id: "1",
role: "User"
},
{
id: "2",
role : "Provider"
},
{
id: "3",
role: "Administration"
}
];

let roleMappings = {
User: "Disable",
Provider: "Read Me",
Administration: "Admin"
};

let result = array.map( value => {
value.role = roleMappings[value.role];
return value;
});
console.log(result);

关于javascript - 如何映射和更改数组内的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54087583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com