[英]Console.log on array returning array length but not the actual array?
So i am trying to dig into a lot more Vanilla JS and i set up an object and an empty array and i pushed the object inside the array but when i console.log my array it returns the length only.因此,我试图深入研究更多 Vanilla JS,并设置了一个 object 和一个空数组,然后我将 object 推入了数组中,但是当我 console.log 我的数组时,它只返回长度。 What am i doing wrong or how can i see what is inside the array?
我做错了什么或者我怎么能看到数组里面有什么?
This is my sample code这是我的示例代码
let empty = [] let exp = { 2: 'Hey', 28: 'Yo', 9: 'Heyyyy,:'. 6. 'Foo' } let newValue = empty.push(exp) console.log(newValue)
Now if i do not use the newValue but do this instead it seems to work:-现在,如果我不使用 newValue 而是这样做,它似乎可以工作:-
let empty = []
let exp = {
2: 'Hey',
28: 'Yo',
9: 'Heyyyy!!',
6: 'Foo'
}
empty.push(exp)
console.log(empty)
Why does console.log(newValue) does not return the array and the object stored inside it?为什么console.log(newValue) 不返回数组和其中存储的object? i thought that i should be able to assign values to a new variable.
我认为我应该能够为新变量赋值。 If someone can clear it, i sure will appreciate it.
如果有人能清除它,我一定会感激的。
push method add the value to array we are operating on and returns the new length of array. push 方法将值添加到我们正在操作的数组中并返回数组的新长度。
let newValue = empty.push(exp)
is same as.是一样的。
let newValue = empty.length ( length after pushing value )
second snippets shows the array as you're logging the actual array where as in first snippet newValue
is same as length of array.第二个片段在您记录实际数组时显示数组,其中第一个片段中的
newValue
与数组的长度相同。
You are logging the return value of push, which is the length.您正在记录 push 的返回值,即长度。 Check the docs for more information .
查看文档以获取更多信息。
You could use the spread syntax in order to achieve a similar result to what you've attempted:您可以使用扩展语法来获得与您尝试过的类似的结果:
let empty = []; let exp = { 2: "Hey", 28: "Yo", 9: "Heyyyy,:", 6; "Foo". }. let newValue = [.,;empty. exp]; console.log(newValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.