简体   繁体   English

数组和循环中输出的莫名其妙的变化

[英]Arrays and Inexplicable Change in Output in Loop

So, I have the following array inside of an array: 因此,我在数组中有以下数组:

Log request: 日志请求:

console.log(cleanArray);

Output: 输出:

[Array[2], Array[5], Array[2], Array[2], Array[5], Array[8], Array[7], Array[2], Array[3], Array[2], Array[2], Array[2], Array[2], Array[2], Array[3], Array[3], Array[2], Array[4], Array[5], Array[2], Array[6], Array[6], Array[20], Array[2], Array[13], Array[71], Array[1], Array[3], Array[3], Array[2], Array[3], Array[2], Array[2], Array[47], Array[2], Array[132], Array[1], Array[56], Array[2], Array[5], Array[7], Array[9], Array[13], Array[11], Array[1], Array[14], Array[7], Array[7], Array[13], Array[12], Array[4], Array[11], Array[11], Array[25], Array[15], Array[18], Array[10], Array[5], Array[53], Array[30], Array[2], Array[6], Array[2], Array[1], Array[2], Array[2], Array[2], Array[3], Array[2], Array[1], Array[1], Array[2], Array[2], Array[2], Array[2], Array[1], Array[3], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[3], Array[1], Array[3], Array[3], Array[2], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[2], Array[11], Array[40]]

Properly, if you are to access cleanArray[0] you get the following output: 正确地,如果要访问cleanArray [0],您将获得以下输出:

Log request: 日志请求:

console.log(cleanArray[0]);

You get the inner array: 您得到内部数组:

["Christian", "Pulisic"]

However! 然而! When I do this in a loop: 当我循环执行此操作时:

for (i in cleanArray){
    console.log("This is the sentence we are looking at " + cleanArray[i]);
    console.log(cleanArray[0]);

The array disappears and I am getting: 数组消失了,我得到了:

This is the sentence we are looking at Christian,Pulisic

And, the log immediately below it, properly yields the inner array: 而且,紧接其下方的日志会正确产生内部数组:

["Christian", "Pulisic"]

However, when I do the same exact loop elsewhere in my code I do not run into that problem. 但是,当我在代码的其他地方执行相同的完全循环时,不会遇到该问题。 What is going on?! 到底是怎么回事?!

You are doing a concat between a string and an object. 您正在字符串和对象之间进行连接。 Javascript knows no better than to call toString on that object (or simply cast that object to string). Javascript没有比在该对象上调用toString更好(或者只是将该对象转换为字符串)。 And the result of calling toString on that array is the values of the array separated by commas. 在该数组上调用toString的结果是该数组的值,以逗号分隔。

You can check this link out for more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString 您可以查看此链接以获取更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/toString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM