underscorejs-sortBy學習


2.17 sortBy

2.17.1 語法

_.sortBy(list, iteratee, [context])

2.17.2 說明

返回一個排序后的list拷貝副本。

  • list為集合,如數組、對象、字符串、arguments等
  • iteratee為排序的依據,可以為function,元素的屬性、元素的key也可以全局方法。
  • iteratee也可以不傳
  • iteratee的參數為(value, key, list)
  • iteratee如果是function需要返回值
  • context可以改變iteratee內部的this
  • 返回的結果是list的副本

2.17.3 代碼示例

示例一: 比較適合於數組的元素是對象,進行排序

var stooges = [{name: 'moe', age: 60}, {name: 'larry', age: 40}, {name: 'curly', age: 50}];
var result = _.sortBy(stooges, 'age');

console.log(result); 
//=> [{age: 40, name: 'larry'}, {age: 50, name: 'curly'}, {age: 60, name: 'moe'}]

示例二:iteratee為排序的依據

上例的iteratee對list元素的key,也可以是list元素的屬性,或全局的方法。

var list = ['2.00', '1.000', '3.0', '-4'];

// iteratee為全局的方法
var arr1 = _.sortBy(list, Math.abs);
console.log(arr1) //=> ["1.000", "2.00", "3.0", "-4"]

// iteratee為元素的屬性
var arr2 = _.sortBy(list, 'length');
console.log(arr2); //=> ["-4", "3.0", "2.00", "1.000"]

示例三:list為集合

//數組
_.sortBy([1, 4, 7, 10, -2]); //=> [-2, 1, 4, 7, 10]

//對象
_.sortBy({a: -2, b: 1, c: 4}); //=>[-2, 1, 4]

//字符串
_.sortBy('45123'); //=>["1", "2", "3", "4", "5"]

//arguments
(function(){
    _.sortBy(arguments); //=>[-2, 1, 4]
}(-2, 1, 4));

示例四:iteratee的參數

_.sortBy(['a', 'b'], function(value, key, list){
    console.log(value, key, list);
    //=> a 0 ["a", "b"]
    //=> b 1 ["a", "b"]
    return value;
});

示例五:context可以改變iteratee內部的this

_.sortBy(['a'], function(value, key, list){
    console.log(this); //=> Object {text: "hello"}
    return value;
}, {text : 'hello'});

示例六: 返回的結果是list的副本

var list = ['1112', '222'];
var result = _.sortBy(list, 'length');

//list沒有被改變
console.log(list); //=> ["1112", "222"]
console.log(result); //=> ["222", "1112"]

2.17.4 list特殊情況返回空數組

console.log(_.sortBy(null));
console.log(_.sortBy(NaN));
console.log(_.sortBy(undefined));
console.log(_.sortBy({}));
console.log(_.sortBy(''));
console.log(_.sortBy(true));
console.log(_.sortBy(false));

2.17.5 和_.map的對比

var list = [undefined, 4, 1, undefined, 3, 2];

var arr1 = _.map(list, function(){
});
var arr2 = _.sortBy(list, function(){
});
console.log(arr1); //=>[undefined, undefined, undefined, undefined, undefined, undefined]
console.log(arr2); //=>[undefined, 4, 1, undefined, 3, 2]

2.17.6 請將列表按at的值倒序輸出(坑)

var list = [{
    title : 1,
    at : 1452691455595
},{
    title : 3,
    at : 1452691505847
},{
    title : 2,
    at : 1452691505347
}];

var result = (function(list){
    //請寫下你的代碼
}(list));

console.log(result);
//=> [{title:3, at:1452691505847},{title:2, at: 1452691505347},{title:1, at:1452691455595}]

2.17.7 請將列表按stopDate的值排序輸出。(坑)

不管是正序或倒序,stopDate的值null的時候,統一放在后面。

var list = [{
    "id": "0",
    "stopDate": null
}, {
    "id": "1",
    "stopDate": "10/06/2014"
}, {
    "id": "2",
    "stopDate": null
}, {
    "id": "3",
    "stopDate": "09/06/2014"
}];

var asc = (function (list) {
    //請寫下你的代碼
}(list));

var desc = (function (list) {
    //請寫下你的代碼
}(list));


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM