简体   繁体   English

Console.log 上的数组返回数组长度但不是实际数组?

[英]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.

相关问题 console.log更改数组长度(容量) - console.log changes array length (capacity) 将数组从 Function 返回到 console.log - Returning Array from a Function to console.log JavaScript array.length返回正确的数字,但console.log(array)为空 - JavaScript array.length returning correct number, but console.log(array) empty console.log(array)返回空,但console.log(array.length)不返回0 - console.log(array) returns empty, but console.log(array.length) doesn't return 0 Console.log(myState) 显示完整数组,但 Console.log(myState.length) 给出 0 - Console.log(myState) shows full array, but Console.log(myState.length) gives 0 计时问题,console.log 显示数组元素但长度为零 - timing issue, console.log show Array element but length is zero Array.length似乎不起作用; console.log显示否则 - Array.length seemingly not working; console.log shows otherwise 为什么最后一个 console.log 中的侦听器数组长度是 3 而不是 2? - Why listeners array length is 3 instead of 2 in the last console.log? 用于返回数组的函数返回undefined。 函数内的Console.log(数组)返回数组 - Function that is intended to return an array is returning undefined. Console.log(array) within function returns the array 阵列console.log结果之间的差异 - difference between array console.log results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM