【问题标题】:What is the difference between these two Javascript arrays?这两个 Javascript 数组有什么区别?
【发布时间】:2018-09-17 13:47:56
【问题描述】:

一个是这样建造的:

var l1 = [23, 50, 63, 90, 10, 30, 155, 23, 18]

另一个是通过浏览器自动化将元素推入其中:

var test = []
list1.each(($el) => {
    return test.push(+$el.get(0).innerText)
})

【问题讨论】:

  • 不清楚您要的是什么。
  • 这只是开发人员工具以不同方式报告它们。它们的结构是相同的。

标签: javascript arrays string numbers


【解决方案1】:

我相信 .each 返回一个带有值的对象。因此,如果您从哪里获取 $el 的类型,它将返回 obj 而不是数字。

【讨论】:

    【解决方案2】:

    第一个在记录之后发生变异,第二个在变异之后被记录。 在控制台中尝试以下操作:

    var arr = [];
    console.log(arr);//will log [] but can expand to an item with name 'one'
    console.table(arr);//the actual value of arr at the time of logging
    console.log(JSON.stringify(arr));//also actual value when logging
    arr.push({name:'one'})
    

    【讨论】:

    • 我试过了,但我在最后两个日志语句中得到了一个空表和空数组。 var arr = []; console.log(arr);//will log [] but can expand to an item with name 'one' console.table(arr);//the actual value of arr at the time of logging console.log(JSON.stringify(arr));//also actual value when logging list1.each(($el) => { arr.push(+$el.get(0).innerText) })
    • @QasimHassan 重点是要显示 console.log 将在控制台中显示该对象,就像对象现在一样(展开并显示推送的项目)。
    猜你喜欢
    • 2021-12-15
    • 2011-02-20
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多