是否保证 JavaScript 数组顺序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34955787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:05:31  来源:igfitidea点击:

Is a JavaScript array order guaranteed?

javascriptarrayspost

提问by Dementic

Assuming a JavaScript array is posted in a form (using ajax/JQuery) is the order in the array guaranteed?

假设一个 JavaScript 数组以一种形式发布(使用 ajax/JQuery)是否保证数组中的顺序?

Valid question from @Quentin "Define 'posted in a form'"

来自@Quentin 的有效问题“定义‘以表格形式发布’”

$.ajax({
  type: "POST",
  url: url,
  data: { foo: [{ name: "one" }, { name: "two" }] },
  success: success,
  dataType: dataType
});

Expanding on the question (because of the above), Is the array guaranteed to preserve the order on the server side?

扩展问题(由于上述原因),数组是否保证在服务器端保留顺序?

回答by Nicola Bizzoca

You can always refer to the ECMAScript standard when in doubts: Array Exotic Objects

如有疑问,您可以随时参考 ECMAScript 标准:Array Exotic Objects

Array is a special kind of object in the language that have additional semantic on how length property is handled respect to the properties that are integers from 0 to 2^32. In javascript an array can be sparse if there are missing values in the range of 0 to the length property excluded. Various array methods take this in consideration, for example forEachignore missing values

数组是语言中的一种特殊对象,它具有关于如何处理长度属性的附加语义,这些属性是从 0 到 2^32 的整数属性。在 javascript 中,如果在 0 到排除的长度属性范围内缺少值,则数组可能是稀疏的。各种数组方法都考虑到了这一点,例如forEach忽略缺失值

What is a missing value?

什么是缺失值?

The language tries to make arrays act as much as possible as to normal objects: you can add any property to it and even have objects that inherit from an array.

该语言试图使数组尽可能地像普通对象一样工作:您可以向它添加任何属性,甚至可以拥有从数组继承的对象。

var fruits = ["Apple", "Banana"];
fruits.preferred = "Apple";

This kind of code don't pose any problem, but if you start to write:

这种代码不会造成任何问题,但是如果您开始编写:

fruits[100] = "Strawberry";
for(let i = 0; i < fruits.length; ++i) {
...
}

100 is a property name in the range of [0, 2^32) so is an array element but at this point what fruits.length should be? It must be 101, otherwise the for loop will never get "Strawberry".

100 是 [0, 2^32) 范围内的属性名称,数组元素也是,但此时fruits.length应该是什么?它必须是 101,否则 for 循环永远不会得到“草莓”。

But at this point you have to accept that there is a range of element [2, 99] that were never defined: these are missing values.

但是此时您必须接受有一个从未定义过的元素 [2, 99] 范围:这些是缺失值。

Vice versa the same must be true when you modify the length property

反之亦然,修改长度属性时也必须如此

var fruits = ["Apple", "Banana"];
fruits.length = 0;

Now a for loop will never get any element of the array: this is equivalent to emptying the fruits array.

现在 for 循环将永远不会获取数组的任何元素:这相当于清空 Fruits 数组。

It's also possible to increment the length, with the result of having missing values

也可以增加长度,结果是缺失值

So yes arrays are ordered as you can iterate all its elements in increasing order, but keep in mind that there can be missing values

所以是的数组是有序的,因为您可以按递增顺序迭代其所有元素,但请记住,可能存在缺失值