简体   繁体   English

JS 多维 Arrays - 如何让 .push() function 正常工作

[英]JS Multidimensional Arrays - How to get the .push() function working correctly

The following code example is what i found here on the internet以下代码示例是我在互联网上找到

arr[1].push('native', 'laravel');

with a description of带有描述

Here we use the javascript array push method to add two new elements(items) to the inner sub-array.这里我们使用 javascript 数组推送方法向内部子数组添加两个新元素(项)。

which was exactly what i wanted to accomplish!这正是我想要完成的!

But when i tried it several times by my self it always went like this:但是当我自己尝试了几次时,它总是这样:

 let mainArr = []; console.log("mainArr: ", mainArr); // Working as expected: created an empty array let emptyArr = []; console.log("emptyArr: ", emptyArr); // Working as expected: created an empty array for (i = 0; i < 3; i++) { mainArr.push(emptyArr); } console.log("mainArr: ", mainArr); // Working as expected: pushed 3 times the "emptyArr" into "mainArr" let newArr = [["X"], [90]]; mainArr[0].push(newArr); console.log("mainArr[0]: ", mainArr[0]); console.log("mainArr[1]: ", mainArr[1]); console.log("mainArr[2]: ", mainArr[2]); // NOT working as expected: pushed "newArr" into every of the three arrays within the "mainArr"?!! // How can I solve this?

Would love to hear some tips:)很想听听一些提示:)

Thanks!谢谢!

That's because arrays are objects in JS, ie non-primitive type of variables.那是因为 arrays 是 JS 中的对象,即非原始类型的变量。

Long explanation长解释

Javascript has 5 data types that are passed by value: Boolean, String, Number, null and undefined. Javascript 有 5 种按值传递的数据类型:Boolean、String、Number、null 和 undefined。 These are called primitive types.这些被称为原始类型。

And it has 3 data types that are passed by reference: Array, Function, and Object.它有 3 种通过引用传递的数据类型:数组、Function 和 Object。 These are all technically Objects.这些都是技术上的对象。

When you assign a non-primitive value to a variable what happens is that a reference of that value is assigned, not the actual value.当您将非原始值分配给变量时,会发生对该值的引用,而不是实际值。 Ie, that reference points to the object's location in memory.即,该引用指向对象在 memory 中的位置。 The variables don't actually contain the value.变量实际上并不包含值。

In your case在你的情况下

When you push emptyArr 3 times you don't insert the value (ie. a new empty array) you insert the same memory reference of emptyArr .当您按emptyArr 3 次时,您不会插入值(即的空数组),而是插入与 emptyArr 相同的emptyArr引用。 These 3 references point to the same location in memory that has that value of an empty array.这 3 个引用指向 memory 中具有空数组值的相同位置。 Given that, now you insert newArr to the referenced value of the mainArr[0] , but the rest elements mainArr[1] , mainArr[2] are references that point to the same value as the one pointed by mainArr[0] .鉴于此,现在您将newArr插入到mainArr[0]的引用值,但 rest 元素mainArr[1]mainArr[2]是指向与mainArr[0]指向的值相同的值的引用。

Hope it makes sense.希望这是有道理的。

Otherwise, You can refer here https://stackoverflow.com/a/430958/4700560 or I also find this article very visually descriptive and helpful https://blog.penjee.com/passing-by-value-vs-by-reference-java-graphical否则,您可以参考这里https://stackoverflow.com/a/430958/4700560或者我也发现这篇文章非常具有视觉描述性和帮助https://blog.penjee.com/passing-by-value-vs-by参考-java-图形

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

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