Nov*_*vis 0 javascript arrays concat
我有四个数组A,BC和D.例如:
length of A is 1
length of b is 4
length of C is 1
length of D is 2.
var a = [1];
var b = [2,3,4,5];
var c = [6];
var d = [7,8];
Run Code Online (Sandbox Code Playgroud)
我想Concat的基于阵列的长度较长的四个阵列,这样的阵列将依次是:b,d,a,c:
预期结果:
[2,3,4,5,7,8,1,6]
[2,3,4,5,1,6,7,8] //also valid, a and c are same length, so can be sorted this way too.
Run Code Online (Sandbox Code Playgroud)
如何在JavaScript中找到较大到较低的数组并将它们从大到小连接起来?
这很简单,使用sort和concat:
Array.prototype.concat.apply([], [a, b, c, d].sort(function (a, b) {
return b.length - a.length;
}));
Run Code Online (Sandbox Code Playgroud)
Array.prototype.concat.apply 用于将子数组连接在一起.
| 归档时间: |
|
| 查看次数: |
109 次 |
| 最近记录: |