javascript数组。与未定义的值排序

时间:2021-11-27 17:40:17

How does Array.prototype.sort handle undefined values in an array?

Array.prototype如何。排序处理数组中未定义的值?

var array = [1,undefined,2,undefined,3,undefined,4];
var array2 = [];
array2[0] = 1;array2[2] = 2;array2[4] = 3;array2[6] = 4;

When calling array.sort(function(l,r) { ... }); The values undefined are never passed in as l or r.

当调用array.sort(函数(l,r){…});未定义的值从不作为l或r传入。

Can I guarantee that all the undefined values will always go to the end of the array for all browsers?

我能保证所有未定义的值都将一直到所有浏览器的数组的末尾吗?

Would the following loop handle all the non undefined data in an array

下面的循环会处理数组中所有未定义的数据吗

array.sort();
for (var i = 0; array[i] !== undefined; i++) {
    // handle array
}

You may assume that no-one declared undefined as a variable.

您可能会假设没有人为未定义的变量。

4 个解决方案

#1


10  

Yes, you can safely assume undefined will get moved to the end of the array.

是的,您可以安全地假设undefined将被移动到数组的末尾。

From MDC:

争取*变革运动:

In JavaScript 1.2, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array

在JavaScript 1.2中,该方法不再将未定义的元素转换为null;相反,它将它们排序到数组的高端

From the spec, 15.4.4.11 :

从规范来看,15.4.4.11:

Because non-existent property values always compare greater than undefined property values, and undefined always compares greater than any other value, undefined property values always sort to the end of the result, followed by non-existent property values.

因为不存在的属性值总是比未定义的属性值大,而未定义的属性值总是比任何其他值大,所以未定义的属性值总是排序到结果的末尾,然后是不存在的属性值。

#2


0  

all undefined values will go to the end of the array regardless of their order in declaration. (at least this is how it works in chrome's js engine)

所有未定义的值都将在数组的末尾,而不考虑它们在声明中的顺序。(至少在chrome的js引擎中是这样工作的)

#3


0  

It appears that the undefined values will fall to the bottom of the list. Here's some sample code to show you what happens:

看起来未定义的值将会下降到列表的底部。下面是一些示例代码,向您展示发生了什么:

var a = [1,undefined,2,undefined,3,undefined,4];
  a = a.sort();
  for( i = 0 ; i < a.length ; i++ )
  {
    alert( a[i] );
  }

This is, of course, the default behavior of JavaScript. If you overwrite the default behavior then only you will know.

当然,这是JavaScript的默认行为。如果重写默认行为,那么只有您知道。

#4


-4  

Safari just deletes undefined property when calling sort(); method

Safari在调用sort()时删除未定义的属性;方法

#1


10  

Yes, you can safely assume undefined will get moved to the end of the array.

是的,您可以安全地假设undefined将被移动到数组的末尾。

From MDC:

争取*变革运动:

In JavaScript 1.2, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array

在JavaScript 1.2中,该方法不再将未定义的元素转换为null;相反,它将它们排序到数组的高端

From the spec, 15.4.4.11 :

从规范来看,15.4.4.11:

Because non-existent property values always compare greater than undefined property values, and undefined always compares greater than any other value, undefined property values always sort to the end of the result, followed by non-existent property values.

因为不存在的属性值总是比未定义的属性值大,而未定义的属性值总是比任何其他值大,所以未定义的属性值总是排序到结果的末尾,然后是不存在的属性值。

#2


0  

all undefined values will go to the end of the array regardless of their order in declaration. (at least this is how it works in chrome's js engine)

所有未定义的值都将在数组的末尾,而不考虑它们在声明中的顺序。(至少在chrome的js引擎中是这样工作的)

#3


0  

It appears that the undefined values will fall to the bottom of the list. Here's some sample code to show you what happens:

看起来未定义的值将会下降到列表的底部。下面是一些示例代码,向您展示发生了什么:

var a = [1,undefined,2,undefined,3,undefined,4];
  a = a.sort();
  for( i = 0 ; i < a.length ; i++ )
  {
    alert( a[i] );
  }

This is, of course, the default behavior of JavaScript. If you overwrite the default behavior then only you will know.

当然,这是JavaScript的默认行为。如果重写默认行为,那么只有您知道。

#4


-4  

Safari just deletes undefined property when calling sort(); method

Safari在调用sort()时删除未定义的属性;方法