Javascript:推入一个空的 3d 数组 [英] Javascript: Pushing in an Empty 3d Array
问题描述
我创建了一个空的 3x3x1 3D 数组
<预><代码>[[[],[],[]],[[],[],[]],[[],[],[]]]现在,位置arr[1][1]
的元素是[]
,所以,如果我执行arr[1][1].push(1)
它应该在 arr[1][1]
1
在JS中有2种方法可以创建一个空的3x3x1数组,代码如下,
var arr1 = [...new Array(3)].map(e => [...new Array(3)].fill([]));var arr2 = [[[],[],[]],[[],[],[]],[[],[],[]]];arr1[1][1].push(1);arr2[1][1].push(1);控制台日志(arr1)控制台日志(arr2)
即.通过快捷方式和另一个手动,arr1
和arr2
应该是一样的,所以应该是输出,但是输出如下,
[[[], [], []], [[1], [1], [1]], [[], [], []]][[[], [], []], [[], [1], []], [[], [], []]]
为什么第一个数组给出这样的输出?两者不一样吗?
我希望输出是第二种形式,如果这是创建空的 3x3x1 数组的错误方法,请提出一种方法,以便提供预期的输出.
再使用一张嵌套映射.我认为 fill
调用将用 same 数组填充第一个 map
在每个第二维中,而不是唯一的,这意味着第二个维度维度都引用同一个数组.
来自 fill 的文档
甚至要注意 value
将完全相同:
用于填充数组的值.(注意数组中的所有元素都将是这个确切的值.)
还有(数组是一个对象):
<块引用>如果第一个参数是一个对象,则数组中的每个槽都将引用该对象.
const arr1 = [...new Array(3)].map(e => [...new Array(3)].充满([]));const arr2 = [[[],[],[]],[[],[],[]],[[],[],[]]];const arr3 = [...new Array(3)].map(() => [...new Array(3)].map(() => []));arr1[1][1].push(1);arr2[1][1].push(1);arr3[1][1].push(1);console.log(JSON.stringify(arr1));//[[[],[],[]],[[1],[1],[1]],[[],[],[]]]console.log(JSON.stringify(arr2));//[[[],[],[]],[[],[1],[]],[[],[],[]]]console.log(JSON.stringify(arr3));//[[[],[],[]],[[],[1],[]],[[],[],[]]]
换句话说:
const expandArr1 = [...new Array(3)].map(() => {/** 来自 MDN 填写文档:* 用于填充数组的值.(注意数组中的所有元素都将是这个确切的值.)*/返回 [...new Array(3)].fill([]);//每个元素都会得到这个确切的值(参考)});//它们是相同的引用:console.log(expandedArr1[1][1] === ExpandedArr1[1][2]);
I create an empty 3x3x1 3D array
[
[[],[],[]],
[[],[],[]],
[[],[],[]]
]
Now, The element at position arr[1][1]
is []
, So, If I execute arr[1][1].push(1)
it should insert 1
on position arr[1][1]
There are 2 methods for creating an empty 3x3x1 array in JS, Here is the code,
var arr1 = [...new Array(3)].map(e => [...new Array(3)].fill([]));
var arr2 = [[[],[],[]],[[],[],[]],[[],[],[]]];
arr1[1][1].push(1);
arr2[1][1].push(1);
console.log(arr1)
console.log(arr2)
ie. Through shortcut and another manually, both arr1
and arr2
should be identical, so should be the output, But the output is as follows,
[[[], [], []], [[1], [1], [1]], [[], [], []]]
[[[], [], []], [[], [1], []], [[], [], []]]
Why is it that first array gives such output? Aren't both identical?
I want the output to be of the 2nd form, If this is a wrong way to create an empty 3x3x1 array, please suggest a method such that it gives expected output.
Use one more nested map. I think that fill
call will fill the first map
with the same array in each of the 2nd dimension instead of a unique one which means the 2nd dimension all reference the same array.
The docs from fill
even note that the value
will be the exact same:
Value to fill the array with. (Note all elements in the array will be this exact value.)
Also (array is an object):
If the first parameter is an object, each slot in the array will reference that object.
const arr1 = [...new Array(3)].map(e => [...new Array(3)].fill([]));
const arr2 = [[[],[],[]],[[],[],[]],[[],[],[]]];
const arr3 = [...new Array(3)].map(() => [...new Array(3)].map(() => []));
arr1[1][1].push(1);
arr2[1][1].push(1);
arr3[1][1].push(1);
console.log(JSON.stringify(arr1)); // [[[],[],[]],[[1],[1],[1]],[[],[],[]]]
console.log(JSON.stringify(arr2)); // [[[],[],[]],[[],[1],[]],[[],[],[]]]
console.log(JSON.stringify(arr3)); // [[[],[],[]],[[],[1],[]],[[],[],[]]]
In other words:
const expandedArr1 = [...new Array(3)].map(() => {
/*
* From MDN fill documentation:
* Value to fill the array with. (Note all elements in the array will be this exact value.)
*/
return [...new Array(3)].fill([]); // Every element will get this exact value (reference)
});
// They are the same reference:
console.log(expandedArr1[1][1] === expandedArr1[1][2]);
这篇关于Javascript:推入一个空的 3d 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!