1. 程式人生 > 其它 >2021.06.18(工作筆錄--關於排序)

2021.06.18(工作筆錄--關於排序)

JS實現陣列排序:升序和降序

如果指明瞭compareFunction,那麼陣列會按照呼叫該函式的返回值排序。即a 和 b 是兩個將要被比較的元素:

  • 如果compareFunction(a, b)小於 0 ,那麼 a 會被排列到 b 之前;

  • 如果compareFunction(a, b)等於 0 , a 和 b 的相對位置不變。備註: ECMAScript標準並不保證這一行為,而且也不是所有瀏覽器都會遵守(例如 Mozilla 在 2003 年之前的版本);

  • 如果compareFunction(a, b)大於 0 , b 會被排列到 a 之前。
  • compareFunction(a, b)
    必須總是對相同的輸入返回相同的比較結果,否則排序的結果將是不確定的。
function compare(a, b) {
  if (a < b ) {           // 按某種排序標準進行比較, a 小於 b
    return -1;
  }
  if (a > b ) {
    return 1;
  }
  // a must be equal to b
  return 0;
}

要比較數字而非字串,比較函式可以簡單的以 a減 b,如下的函式將會將陣列升序排列

function compareNumbers(a, b) { //升序
  return a - b;
}
function compareNumbers(a, b) { //降序
  return b-a;
}

sort方法可以使用函式表示式方便地書寫:

var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
});
console.log(numbers);

也可以寫成:
var numbers = [4, 2, 5, 1, 3]; 
numbers.sort((a, b) => a - b); 
console.log(numbers);

// [1, 2, 3, 4, 5]

物件可以按照某個屬性排序:

var items = [
  { name: 'Edward', value: 21 },
  { name: 'Sharpe', value: 37 },
  { name: 'And', value: 45 },
  { name: 'The', value: -12 },
  { name: 'Magnetic' },
  { name: 'Zeros', value: 37 }
];

// sort by value
items.sort(function (a, b) {
  return (a.value - b.value)
});

// sort by name
items.sort(function(a, b) {
  var nameA = a.name.toUpperCase(); // ignore upper and lowercase
  var nameB = b.name.toUpperCase(); // ignore upper and lowercase
  if (nameA < nameB) {
    return -1;
  }
  if (nameA > nameB) {
    return 1;
  }

  
// names must be equal

  return 0;
});

js 陣列 map方法

map()經常用來遍歷資料。

map()的作用就是“對映”,也就是原陣列被“對映”成對應新陣列。

map() 不會改變原始陣列。

var arr = ["a","b","c","d","e"];
        arr.map(function(currentValue,index,arr){
            console.log("當前元素"+currentValue)
       console.log("當前索引"+index)
            console.log("陣列物件"+arr)
        })

map的引數:

currentValue必須。當前元素的值

index可選。當期元素的索引值

arr可選。當期元素屬於的陣列物件

map() 方法返回一個新陣列,陣列中的元素為原始陣列元素呼叫函式處理後的值。

map() 方法按照原始陣列元素順序依次處理元素。

該方法不會改變原陣列

const array1 = [22, 3, 31, 12, 'arr', 19, 31, 56, 43];
const array2 = array1.map((v, i, a) => {
  return v + 1;
});

console.log(array1); // [22, 3, 31, 12, "arr", 19, 31, 56, 43]
console.log(array2); // [23, 4, 32, 13, "arr1", 20, 32, 57, 44]

工作例項

<script>
        var ethnum = [
            "eth0", "eth1", "eth10", "eth11", "eth12", "eth13", "eth2", "eth3", "eth4", "eth5", "eth6", "eth7", "eth8", "eth9"
        ];
        var ethnum1 = [],
            ethnum2 = [];
        $(ethnum).each(function(i, d){
            ethnum1.push(d.substring(3));
        });
        console.log(ethnum1);    //["0", "1", "10", "11", "12", "13", "2", "3", "4", "5", "6", "7", "8", "9"]
        ethnum1.sort(function(a,b){
            return a - b;
        })
        console.log(ethnum1.sort(function(a,b){
            return a - b;
        }));    //["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"]
        ethnum2 = ethnum1.map((v, i, a) =>{
            return 'eth' + v;
        });
        console.log(ethnum2);  //["eth0", "eth1", "eth2", "eth3", "eth4", "eth5", "eth6", "eth7", "eth8", "eth9", "eth10", "eth11", "eth12", "eth13"]
    </script>

2021-06-18 10:50:40