javascript - 了解 javascript 高级语法和对象表示法

标签 javascript arrays algorithm oop boolean

所以我可以通过这个简洁的辅助函数并且对语法感到困惑。有一个变量(bool声明为true,看起来是一个数组。它使用括号对象表示法,但是然后要比较bool[j]或[i]是否为真,没有添加[i]或[j] 到对象映射。

const helper = (word, words) => {

    let bool = [true]; 
   //if you console.log(typeof bool) returns object ?? 




    //This comes out as an Obj at first glance I thought it was an arr, but its bracket notation
    for (var i = 1; i <= word.length; i++) {
     
        for (var j = 0; j <= i; j++) {
          //how is bool[j] being evaluated? or i if its an obj? 
            if (bool[j] === true && words[word.substring(j, i)] === true) {
                bool[i] = true;
                break;
            } else {
                bool[i] = false;
            }

        }
    }
    return console.log(bool[word.length] ? true : false);
}


helper('aa', ['aa', 'aabb', 'someotherword']);

最佳答案

在 JavaScript 中,数组是 Object 的一个实例,它们的类型将如此注册。

像这样的语句

foo[i] = 'bar'

将在数组(或对象)foo 的第 i 个从零开始的索引处分配值“bar”。这发生在您发布的代码中的 for 循环中。

var a = []

console.log(a instanceof Object)

a[3] = 4

console.log(a)

关于javascript - 了解 javascript 高级语法和对象表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55083891/

相关文章:

javascript - 如何对看似 "fixed"的图像使用视差滚动?

javascript - 如何连接到 mongoDB 并测试 drop 收集?

java - 为什么从变量获取值比从数组获取值更受欢迎?

c++ - 使用 2 "double"s 模拟 "float"

javascript - "this"返回函数而不是对象

Python:当一个数组转置时,numpy 数组是否链接?

javascript - 将 JavaScript 对象(数组)转换为 XML

sql - 如何生成字符数组的二进制组合

algorithm - 编程实验

javascript - 如何使用 AngularJS 获取用户的邮政编码