gpt4 book ai didi

javascript - 如何在 Javascript 的 .forEach 循环中设置索引参数的值?

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

关于如何动态设置字段的值,我有以下建议:

Can I set the value of a property dynamically?

var data = {roles: "Admin:Xxxx:Data"};
var user = {data: {role:{}}};

data.roles.split(':').forEach(function(v) {
user.data.role['is' + v] = true;
})

这对我来说非常有效。

现在我需要根据不同 Angular 色的存在或不存在来设置另一个参数的值。公式为

index = 0 if the Test role is present
index = index + 1 if the Xxxx role is present
index = index + 2 if the Yyyy role is present
index = index + 100 if the Data role is present

我希望得到一些关于如何将其结合到我现有的解决方案中的建议。如果可能的话,我想制定解决方案,以便以后可以扩展更多 Angular 色。如果可能的话,我希望避免编写多个 if 语句。

最佳答案

使用对象。

var indexes = {'Xxxx':0,'Yyyy':index+1,'Data':index+100};

index=indexes['Xxxx'];

关于javascript - 如何在 Javascript 的 .forEach 循环中设置索引参数的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24300210/

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