gpt4 book ai didi

javascript - 把map当做for循环不返回可以吗

转载 作者:行者123 更新时间:2023-11-29 22:51:54 24 4
gpt4 key购买 nike

我在我的项目中到处都使用 map ,即使我不想返回任何东西,我只是使用 {} 而不是 () 并且不返回任何东西,只需像 for 循环 一样使用它,这样可以吗?如果是,不使用任何键怎么办?因为我有时并不真正使用 map 中的元素,我什至不需要(或不知道如何)使用键!
这是我从我的代码中复制的一个例子:

rawElementList.map(element => {
if (element.validation !== undefined) {
if (JavascriptUtils.hasOwnProperty(element.validation, 'isRequired')) {
const isRequired = element.validation.isRequired;

if (isRequired !== undefined) {
delete element.validation.isRequired;
required.push(String(element.id))
}
}

validationJsonList.properties[element.id] = element.validation;
}
})

最佳答案

不,那不合适。如果您使用专门设计用于创建数组的方法,但您实际上并未创建有意义的数组,那么您将极大地混淆您的代码的任何其他读者。改用 forEach - 它是最适合副作用的数组方法:

rawElementList.forEach(element => {
if (element.validation !== undefined) {
if (JavascriptUtils.hasOwnProperty(element.validation, 'isRequired')) {
const isRequired = element.validation.isRequired;

if (isRequired !== undefined) {
delete element.validation.isRequired;
required.push(String(element.id))
}
}

validationJsonList.properties[element.id] = element.validation;
}
})

关于javascript - 把map当做for循环不返回可以吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57344542/

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