通过解构赋值获取数组长度

huangapple go评论53阅读模式
英文:

Getting array length by destructuring assignment

问题

以下是翻译好的部分:

在阅读了MDN文档中有关Destructuring assignment的示例后,有一个示例如下:

const [a, b, ...{ pop, push }] = [1, 2];
console.log(a, b); // 1 2
console.log(pop, push); // [Function pop] [Function push]

它说:“这允许您同时解构数组的属性和索引。”

我想了一下,如果我能够在同时获取索引值的同时获得数组的长度,那不是很有趣吗?因此,我在控制台中尝试了以下代码:

const arr = [1, 2]
const [x, y, ...{length}] = arr
console.log(x) // 1
console.log(y) // 2
console.log(length) // 0 | ??? 难道不应该是2吗?

以及:

// 那么这个呢...
const arr = [1, 2]
const {length} = arr
console.log(length) // 2 | 好吧,这种方式有效,但为什么解构赋值不行呢?

那么,有谁知道为什么在第一个解构赋值中length没有被赋值为2吗?还是我发现了JavaScript中的一个错误?

英文:

After reading the MDN doc with examples of Destructuring assignment, and there is an example like:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const [a, b, ...{ pop, push }] = [1, 2];
console.log(a, b); // 1 2
console.log(pop, push); // [Function pop] [Function push]

<!-- end snippet -->

and it says: This allows you to simultaneously unpack the properties and indices of arrays.

I took a second thought, isn't it will be fascinating if I could get the length of the array by doing this assignment at the same time getting the indices values out, so I tried this in my console:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr = [1, 2]
const [x, y, ...{length}] = arr
console.log(x) // 1
console.log(y) // 2
console.log(length) // 0 | ??? isn&#39;t it should give me 2?

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// how about this...
const arr = [1, 2]
const {length} = arr
console.log(length) // 2 | ok well, it did work this way, but why not the destructuring assignment?

<!-- end snippet -->

So, does anybody know why the length did not got assigned with value 2 in the first destructuring assignment? or did I found a bug in JavaScript?

答案1

得分: 6

这段代码的作用是:

[a, b, ...something] = someArray

它从 someArray 中取出剩余的元素(从索引2开始),并将它们放入一个名为 something 的数组中。

执行以下代码:

...{ pop, push }

从该数组中取出两个属性:Array.prototype.popArray.prototype.push。但因为原始数组只有2个元素,所以执行以下代码:

...{ length }

将会得到0。

如果原始数组有更多的项,你将看到一个长度。例如:

const arr = [1, 2, 3, 4, 5]
const [x, y, ...{length}] = arr;

console.log(length);

前两个项被放入 xy 中。最后三个项被作为一个数组提取,然后从该数组中解构出 length 属性,所以长度为3。

要获取实际长度,可以将之前解构的项的数量添加到长度中,如下:

const arr = [1, 2]
const [x, y, ...{length}] = arr
const actualLength = length + 2; // 因为解构了2个项:x 和 y
console.log(x) // 1
console.log(y) // 2
console.log(actualLength);

或者使用对象解构语法,如下:

const arr = [1, 2]
const { 0: x, 1: y, length } = arr;
console.log(x) // 1
console.log(y) // 2
console.log(length);

或者更容易理解的方式,只需在单独的行上执行 const { length } = arr

英文:

What this does

[a, b, ...something] = someArray

is it takes the rest of the elements (index 2 and after) from someArray and puts them into a single variable as an array.

Doing

...{ pop, push }

takes two properties from that array: Array.prototype.pop and Array.prototype.push. But there aren't any more elements, because the original array had only 2 items, so doing

...{ length }

will give you 0.

If the original array did have more items, you'd see a length. For example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr = [1, 2, 3, 4, 5]
const [x, y, ...{length}] = arr;

console.log(length);

<!-- end snippet -->

The first two items are put into x and y. The last three items are taken as an array and then the length property is destructured from that array - so the length is 3.

Either add the number of destructured items previously to the length to get the actual length

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr = [1, 2]
const [x, y, ...{length}] = arr
const actualLength = length + 2; // because 2 items were destructured; x and y
console.log(x) // 1
console.log(y) // 2
console.log(actualLength);

<!-- end snippet -->

Or use object destructuring syntax instead

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr = [1, 2]
const { 0: x, 1: y, length } = arr;
console.log(x) // 1
console.log(y) // 2
console.log(length);

<!-- end snippet -->

Or, more understandable at a glance than either of the above - just do const { length } = arr on a separate line.

答案2

得分: 0

让我们看看当你只移除一个元素时会发生什么

const arr = [1, 2]
const [x, ...{length}] = arr
console.log("x", x) // 1
console.log("length", length) // 1

请注意,长度为1,因为你从2个元素开始,然后去掉1个,2 - 1 等于1。

为什么会这样?... 表示"rest",因此它是其余的属性。

const arr = [1, 2]
const [x, ...rest] = arr
console.log("x", x) // 1
console.log("rest", rest) // [2]

当你使用{} 时,你会从rest中获取一个属性,所以这与以下代码相同:

const arr = [1, 2]
const [x, ...rest] = arr
console.log("x", x) // 1
const {length} = rest;
console.log("rest", rest) // [2]
console.log("length", length) // 1
英文:

Let's see what happens when you only take one element off

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr = [1, 2]
const [x, ...{length}] = arr
console.log(&quot;x&quot;, x) // 1
console.log(&quot;length&quot;, length) // 1

<!-- end snippet -->

Notice how the length is one, because you start with 2 elements, then get rid of 1, and 2 - 1 is 1.

Why does this happen? ... means "rest", so it's the rest of the properties.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr = [1, 2]
const [x, ...rest] = arr
console.log(&quot;x&quot;, x) // 1
console.log(&quot;rest&quot;, rest) // [2]

<!-- end snippet -->

When you use { } you get a property off of the rest, so it's the same as saying

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr = [1, 2]
const [x, ...rest] = arr
console.log(&quot;x&quot;, x) // 1
const {length} = rest;
console.log(&quot;rest&quot;, rest) // [2]
console.log(&quot;length&quot;, length) // 1

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 08:24:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定