【问题标题】:Get access to the right element in the Array访问数组中的正确元素
【发布时间】:2017-06-19 14:09:18
【问题描述】:

我正在使用下面的 for 循环:

console.log(a1); 
 for(var f = 0; f < a1.length; f++){

 b= a1[f];

console.log(b);............

我的控制台显示了这个:

  Array [ Array[6], Array[8] ] 
  Array [ Object, Object, Object, Object, Object, Object ] 
  Array [ Object, Object, Object, Object, Object, Object, Object, Object]

现在,控制台中的第一行数组是console.log(a1),其他两个数组是console.log(b)

我只想访问console.log(b) 的整个第一个数组。 我尝试使用 console.log(b[0]),但这仅向我显示了第一个对象,包括它们的 2 个数组的属性,但我只想在我的控制台上查看包含 6 个元素的完整第一个数组。有人可以帮我解决吗?

【问题讨论】:

    标签: javascript arrays string for-loop console


    【解决方案1】:

    据我了解,您希望使用第一个对象数组,它是 a1 的第一个元素,因此您需要遍历 a1[0],它会为您提供所需的第一个数组:

    for(var f = 0; f < a1[0].length; f++) {
        var b = a1[0][f];
        console.log(b);
        //do stuff here
    }
    

    【讨论】:

    • 不幸的是,这不适用于我之后使用的代码。因为当我使用您的解决方案时,它会给我一个错误,例如“b.includes 不是函数”。有什么解决方案可以让数组 b 保持原样并且只改变循环?
    • 那是因为b 不再是一个数组。 b 是第一个数组的 元素。因为a1 是一个数组数组,a1[0] 是你的第一个数组,a1[1] 是你的第二个数组。但是,如果您让我们知道您想在代码中进一步完成什么,我们或许可以提供帮助。
    • @derick collin 不是你的问题的一部分 => 不是答案的一部分
    • @Botimoo,感谢您的帮助。进一步的代码只是 d3.js 中节点的一些样式,我在其中使用函数 b.includes()
    猜你喜欢
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    相关资源
    最近更新 更多