Array.prototype.fill() 与填充不同 [英] Array.prototype.fill() different from fill as I go

查看:21
本文介绍了Array.prototype.fill() 与填充不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决一个将结果输出到二维数组的问题,并在每个元素进行时添加一个.

简化了代码以创建测试用例.如果我按照我的方式填充数组,如下所示:

var a = [[], [], [] ,[] ,[], []];无功d1,d2;对于 (d1 = 0; d1 <6; d1++) {对于 (d2 = 0; d2 <6; d2++) {a[d1][d2] = (a[d1][d2]) ?(a[d1][d2]) + 1 : 1;}}

我得到一个二维数组,其中所有的值都是 1.但是,如果我使用 array.prototype.fill() 预填充数组,如下所示:

var a = new Array(6).fill(new Array(6).fill(0));无功d1,d2;对于 (d1 = 0; d1 <6; d1++) {对于 (d2 = 0; d2 <6; d2++) {a[d1][d2] += 1;}}

我得到一个 6s 的二维数组.即使我用旧的 a[d1][d2] = (a[d1][d2]) 替换 a[d1][d2] += 1 ?(a[d1][d2]) + 1 : 1;,(应该仍然有效,因为 0 和 undefined 都是假的)我仍然得到 6s.

据我所知,我的代码应该循环遍历每个元素并将一个添加到前一个值.它不应该多次接触任何元素,所以它们都应该是 1s.我是否提前填充数组无关紧要.

我理解的失败在哪里?

解决方案

Array.fill 旨在用于使用 static 值填充数组从开始索引到结束索引的所有元素.>

不幸的是,这意味着如果你传入一个元素,比如一个新数组,你的数组实际上会被许多对同一元素的引用填充.换句话说,a 没有填充六个大小为 6 的数组;a 填充了六个指向 相同数组 大小为 6 的指针.

您可以在开发者控制台中轻松验证这一点:

var a = new Array(6).fill(new Array(6).fill(0));一种>>>[数组[6],数组[6],数组[6],数组[6],数组[6],数组[6]][0]>>>[0, 0, 0, 0, 0, 0][1]>>>[0, 0, 0, 0, 0, 0][0][1] = 1[0]>>>[0, 1, 0, 0, 0, 0][1]>>>[0, 1, 0, 0, 0, 0]

I am working on a problem that outputs its results into a 2D array, adding one to each element as it goes.

I simplified the code down as far as I could to create a test case. If I fill the array as a I go, as follows:

var a = [[], [], [] ,[] ,[], []];
var d1, d2;
for (d1 = 0; d1 < 6; d1++) {
    for (d2 = 0; d2 < 6; d2++) {
        a[d1][d2] = (a[d1][d2]) ? (a[d1][d2]) + 1 : 1; 
    }
}

I get a 2D array where all the values are 1. However, if I prefill the array using array.prototype.fill() as follows:

var a = new Array(6).fill(new Array(6).fill(0));
var d1, d2;
for (d1 = 0; d1 < 6; d1++) {
    for (d2 = 0; d2 < 6; d2++) {
        a[d1][d2] += 1; 
    }
}

I get a 2D array full of 6s. Even if I replace a[d1][d2] += 1 with the old a[d1][d2] = (a[d1][d2]) ? (a[d1][d2]) + 1 : 1;, (which should still work since both 0 and undefined are falsy) I still get 6s.

As far as I can tell, my code should just loop through each element and add one to the previous value. It shouldn't touch any element more than once, so they should all be 1s. Whether I fill the array ahead of time or not shouldn't matter.

Where is the failure in my understanding?

解决方案

Array.fill is intended to be used to fill all the elements of an array from a start index to an end index with a static value.

This unfortunately means that if you pass in an element such as a new array, your array will actually be filled with many references to that same element. In other words, a is not filled with six arrays of size six; a is filled with six pointers to the same array of size six.

You can easily verify this in your developer console:

var a = new Array(6).fill(new Array(6).fill(0));
a
>>> [Array[6], Array[6], Array[6], Array[6], Array[6], Array[6]]
a[0]
>>> [0, 0, 0, 0, 0, 0]
a[1]
>>> [0, 0, 0, 0, 0, 0]
a[0][1] = 1
a[0]
>>> [0, 1, 0, 0, 0, 0]
a[1]
>>> [0, 1, 0, 0, 0, 0]

这篇关于Array.prototype.fill() 与填充不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆