我有以下二维数组: [["fox", "100"], ["the", "1"], ["quick", "50"]]
并且只想存储数组的第一个元素,但根据第二个值进行排序。
期望的输出: the,quick,fox
我写了一个循环,它遍历第一个似乎有效的元素,但是我无法根据第二个值对其进行排序:
for (var i = 0; i < inputArr.length; i++) {
x1 += inputArr[i][0];
if(i != inputArr.length - 1){
x1 = x1 + ",";
}
}
Write(x1); //outputs -> fox,the,quick
您可以使用sort
Array 的功能。该sort
函数采用基于返回值的 2 个输入的 lambda 函数决定顺序
let arr = [["fox", "100"], ["the", "1"], ["quick", "50"]];
let output= arr.sort((a,b) => a[1]-b[1]).map(e => e[0]).join(",");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句