![](/img/trans.png)
[英]Javascript, concatenate 1st element of an array to 1st element of another array
[英]JS return 1st element of 2d array sorted
我有以下二維數組: [["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
您可以使用 Array 的sort
function。 sort
function 采用 lambda function 根據返回值決定順序
let arr = [["fox", "100"], ["the", "1"], ["quick", "50"]]; let output= arr.sort((a,b) => a[1]-b[1]).map(e => e[0]).join(",");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.