对象字面量中的 Javascript 变量范围

问题描述

一个新手问题..这是我的代码

let result = (...ohStr) => {
    let res = []; // edited (before res = [])
    for (const Str of ohStr){
        if(/oh{3,6} no/.test(Str)){
            res.push({Str:true});
        }
        else{
            res.push({Str:false});
        }
    }
        return res;
}

console.log(result("ohhh no","ohhhh no","ohhhhh no","oh no","ohh no"));

问题是为什么 res 数组是这样创建的:

[ { Str: true },{ Str: true },{ Str: false },{ Str: false } ]

而是在 for 循环中的每次迭代中添加 Str 值?以及我是如何克服这个问题的?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)