Tách array chính ra thành những array con với số lượng nhất định.
_.chunk (array, [size=1]) _.chunk(['a', 'b', 'c', 'd'], 2); // => [['a', 'b'], ['c', 'd']] _.chunk(['a', 'b', 'c', 'd'], 3); // => [['a', 'b', 'c'], ['d']]
Tạo những array theo vị trí của các array.
_.unzip(array) var zipped = _.zip(['a', 'b'], [1, 2], [true, false]); // => [['a', 1, true], ['b', 2, false]] _.unzip(zipped); // => [['a', 'b'], [1, 2], [true, false]]
Cắt các chữ trong string bỏ vào mảng.
_.words([string=''], [pattern])
_.words('fred, barney, & pebbles');
// => ['fred', 'barney', 'pebbles']
_.words('fred, barney, & pebbles', /[^, ]+/g);
// => ['fred', 'barney', '&', 'pebbles']
Xóa những dữ liệu là false khỏi array.
Những dữ liệu là false:
_.compact (array) _.compact([0, 1, false, 2, '', 3]);
Cắt n phần tử từ vị trí đầu tiên.
_.drop(array, [n=1]) _.drop([1, 2, 3]); // => [2, 3] _.drop([1, 2, 3], 2); // => [3] _.drop([1, 2, 3], 5); // => [] _.drop([1, 2, 3], 0); // => [1, 2, 3]
Cắt n phần tử từ vị trí cuối cùng.
_.dropRight (array, [n=1]) _.dropRight([1, 2, 3]); // => [1, 2] _.dropRight([1, 2, 3], 2); // => [1] _.dropRight([1, 2, 3], 5); // => [] _.dropRight([1, 2, 3], 0); // => [1, 2, 3]
Cắt n phần tử từ vị trí cuối cùng với điều kiện tạo đó.
_.dropRightWhile(array, [predicate=_.identity])
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.dropRightWhile(users, function(o) { return !o.active; });
// => objects for ['barney']
// The `_.matches` iteratee shorthand.
_.dropRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['barney', 'fred']
// The `_.matchesProperty` iteratee shorthand.
_.dropRightWhile(users, ['active', false]);
// => objects for ['barney']
// The `_.property` iteratee shorthand.
_.dropRightWhile(users, 'active');
// => objects for ['barney', 'fred', 'pebbles']
Cắt n phần tử từ vị trí đầu tiên với điều kiện tạo đó.
_.dropWhile(array, [predicate=_.identity])
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
_.dropWhile(users, function(o) { return !o.active; });
// => objects for ['pebbles']
// The `_.matches` iteratee shorthand.
_.dropWhile(users, { 'user': 'barney', 'active': false });
// => objects for ['fred', 'pebbles']
// The `_.matchesProperty` iteratee shorthand.
_.dropWhile(users, ['active', false]);
// => objects for ['pebbles']
// The `_.property` iteratee shorthand.
_.dropWhile(users, 'active');
// => objects for ['barney', 'fred', 'pebbles']
Bỏ phần tử cuối cùng của mảng.
_.initial(array) _.initial([1, 2, 3]); // => [1, 2]
Xóa các item có giá trị bằng với item value khỏi mảng.
_.pull(array, [values]) var array = ['a', 'b', 'c', 'a', 'b', 'c']; _.pull(array, 'a', 'c'); console.log(array); // => ['b', 'b']
Giống _.pull nhưng value dạng array.
_.pullAll(array, values) var array = ['a', 'b', 'c', 'a', 'b', 'c']; _.pullAll(array, ['a', 'c']); console.log(array); // => ['b', 'b']
Giống _.pull nhưng kèm điều kiện.
_.pullAllBy(array, values, [iteratee=_.identity])
var array = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }];
_.pullAllBy(array, [{ 'x': 1 }, { 'x': 3 }], 'x');
console.log(array);
// => [{ 'x': 2 }]
Loại bỏ item tại các index.
_.pullAt(array, [indexes]) var array = ['a', 'b', 'c', 'd']; var pulled = _.pullAt(array, [1, 3]); console.log(array); // => ['a', 'c'] console.log(pulled); // => ['b', 'd']
Xóa theo điều kiến. Xóa luôn item trong array gốc.
_.remove(array, [predicate=_.identity])
var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) {
return n % 2 == 0;
});
console.log(array);
// => [1, 3]
console.log(evens);
// => [2, 4]
Xóa item có trong mảng giống với các value.
_.without(array, [values]) _.without([2, 1, 2, 3], 1, 2); // => [3]
Bỏ phần tử trùng, tối ưu hóa cho mảng đã sắp xếp.
_.sortedUniq(array) _.sortedUniq([1, 1, 2]); // => [1, 2]
Bỏ phần tử trùng, tối ưu hóa cho mảng đã sắp xếp. Nhưng có thêm điều kiện.
_.sortedUniqBy(array, [iteratee]) _.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor); // => [1.1, 2.3]
Bỏ khoảng trắng hai đầu.
_.trim([string=''], [chars=whitespace])
_.trim(' abc ');
// => 'abc'
_.trim('-_-abc-_-', '_-');
// => 'abc'
_.map([' foo ', ' bar '], _.trim);
// => ['foo', 'bar']
Bỏ khoảng trắng bên trái.
_.trimStart([string=''], [chars=whitespace])
_.trimStart(' abc ');
// => 'abc '
_.trimStart('-_-abc-_-', '_-');
// => 'abc-_-'
Bỏ khoảng trắng bên phải.
_.trimEnd([string=''], [chars=whitespace])
_.trimEnd(' abc ');
// => ' abc'
_.trimEnd('-_-abc-_-', '_-');
// => '-_-abc'
Cắt string và thêm dấu “…” vào nếu srting dài quá length.
_.truncate([string=''], [options={}])
_.truncate('hi-diddly-ho there, neighborino');
// => 'hi-diddly-ho there, neighbo...'
_.truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator': ' '
});
// => 'hi-diddly-ho there,...'
_.truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator': /,? +/
});
// => 'hi-diddly-ho there...'
_.truncate('hi-diddly-ho there, neighborino', {
'omission': ' [...]'
});
// => 'hi-diddly-ho there, neig [...]'
Nối các phần tử của một hoặc nhiều mảng vào mảng khác.
_.concat (array, [values]) var array = [1]; var other = _.concat(array, 2, [3], [[4]]); console.log(other); // => [1, 2, 3, [4]] console.log(array); // => [1]
Nối các phần tử lại bằng ký tự gì đó.
_.join(array, [separator=',']) _.join(['a', 'b', 'c'], '~'); // => 'a~b~c'
Gom những vị trí lại thành một mảng.
_.zip([arrays]) _.zip(['a', 'b'], [1, 2], [true, false]); // => [['a', 1, true], ['b', 2, false]]
Giống trên nhưng có xử lý thêm điều kiện gì đó.
_.zipWith([arrays], [iteratee=_.identity])
_.zipWith([1, 2], [10, 20], [100, 200], function(a, b, c) {
return a + b + c;
});
// => [111, 222]
Gộp object thành 1 object, không ghi đè undefined.
_.merge(object, [sources])
var object = {
'a': [{ 'b': 2 }, { 'd': 4 }]
};
var other = {
'a': [{ 'c': 3 }, { 'e': 5 }]
};
_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
Gộp object thành 1 object, có ghi đè undefined.
_.assign(object, [sources])
function Foo() {
this.a = 1;
}
function Bar() {
this.c = 3;
}
Foo.prototype.b = 2;
Bar.prototype.d = 4;
_.assign({ 'a': 0 }, new Foo, new Bar);
// => { 'a': 1, 'c': 3 }
Lấy các item có trong mảng này mà không có không mảng kia.
_.difference (array, [values]) _.difference([2, 1], [2, 3]); // => [1]
Tương tự ._difference nhưng nó sẽ áp dụng tham số thứ 3 apply vào hoặc làm điều kiện so sánh.
_.differenceBy(array, [values], [iteratee=_.identity])
_.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [1.2]
/* giải thích
Hàm Math.floor => bỏ dấu thập phân
=> [2, 1] compare [2, 3]
=> difference [1]
=> result tương ứng là [1.2]
*/
// The `_.property` iteratee shorthand.
_.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
// => [{ 'x': 2 }]
/* giải thích
Chọn các phần tử có thuộc tính 'x'
=> result // => [{ 'x': 2 }]
*/
Lấy phần tử đầu tiên của mảng.
Giống với _.first(array)
_.head(array) _.head([1, 2, 3]); // => 1 _.head([]); // => undefined
Lấy những phần tử giống nhau của các mảng.
_.intersection([arrays]) _.intersection([2, 1], [2, 3]); // => [2]
Giống intersection nhưng nó sẽ áp dụng tham số thứ 3 apply vào hoặc làm điều kiện so sánh.
_.intersectionBy([arrays], [iteratee=_.identity])
_.intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [2.1]
// The `_.property` iteratee shorthand.
_.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }]
Lấy phần tử cuối cùng của array.
_.last(array) _.last([1, 2, 3]); // => 3
Tìm phần tử cuối cùng giống value search.
_.lastIndexOf(array, value, [fromIndex=array.length-1]) _.lastIndexOf([1, 2, 1, 2], 2); // => 3 // Search from the `fromIndex`. _.lastIndexOf([1, 2, 1, 2], 2, 2); // => 1
Lấy phần tử thứ i của array.
_.nth(array, [n=0]) var array = ['a', 'b', 'c', 'd']; _.nth(array, 1); // => 'b' _.nth(array, -2); // => 'c';
Lấy từ đâu tới đâu, không bao gồm phần tử cuối.
_.slice(array, [start=0], [end=array.length])
Lấy tất cả nhưng bỏ phần tử đầu.
_.tail(array) _.tail([1, 2, 3]); // => [2, 3]
Tạo một mảng với n phần tử cắt từ vị trí đầu tiên.
_.take(array, [n=1]) _.take([1, 2, 3]); // => [1] _.take([1, 2, 3], 2); // => [1, 2] _.take([1, 2, 3], 5); // => [1, 2, 3] _.take([1, 2, 3], 0); // => []
Tạo một mảng với n phần tử cắt từ vị trí cuối cùng.
_.takeRight(array, [n=1]) _.takeRight([1, 2, 3]); // => [3] _.takeRight([1, 2, 3], 2); // => [2, 3] _.takeRight([1, 2, 3], 5); // => [1, 2, 3] _.takeRight([1, 2, 3], 0); // => []
Lấy các phần tử theo điều kiện từ vị trí cuối.
_.takeRightWhile(array, [predicate=_.identity])
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.takeRightWhile(users, function(o) { return !o.active; });
// => objects for ['fred', 'pebbles']
// The `_.matches` iteratee shorthand.
_.takeRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['pebbles']
// The `_.matchesProperty` iteratee shorthand.
_.takeRightWhile(users, ['active', false]);
// => objects for ['fred', 'pebbles']
// The `_.property` iteratee shorthand.
_.takeRightWhile(users, 'active');
// => []
Lấy các phần tử theo điều kiện bắt đầu từ vị trí đầu tiên.
_.takeWhile(array, [predicate=_.identity])
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
_.takeWhile(users, function(o) { return !o.active; });
// => objects for ['barney', 'fred']
// The `_.matches` iteratee shorthand.
_.takeWhile(users, { 'user': 'barney', 'active': false });
// => objects for ['barney']
// The `_.matchesProperty` iteratee shorthand.
_.takeWhile(users, ['active', false]);
// => objects for ['barney', 'fred']
// The `_.property` iteratee shorthand.
_.takeWhile(users, 'active');
// => []
Lấy những item khác nhau của những array.
_.xor([arrays]) _.xor([2, 1], [2, 3]); // => [1, 3]
Giống trên nhưng có xử lý thêm gì đó.
_.xorBy([arrays], [iteratee=_.identity])
_.xorBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [1.2, 3.4]
// The `_.property` iteratee shorthand.
_.xorBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 2 }]
Giống trên nhưng có thêm điều kiện.
_.xorWith([arrays], [comparator])
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.xorWith(objects, others, _.isEqual);
// => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
Giống trên nhưng có thêm điều kiện.
_.size(collection)
_.size([1, 2, 3]);
// => 3
_.size({ 'a': 1, 'b': 2 });
// => 2
_.size('pebbles');
// => 7
Lấy dữ liệu dễ hơn dạng mảng và object.
_.keyBy(collection, [iteratee=_.identity])
var array = [
{ 'dir': 'left', 'code': 97 },
{ 'dir': 'right', 'code': 100 }
];
_.keyBy(array, function(o) {
return String.fromCharCode(o.code);
});
// => { 'a': { 'dir': 'left', 'code': 97 }, 'd': { 'dir': 'right', 'code': 100 } }
_.keyBy(array, 'dir');
// => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
Trả về array chứa các element không thỏa điều kiện của tham số thứ hai.
_.reject(collection, [predicate=_.identity])
var users = [
{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': true }
];
_.reject(users, function(o) { return !o.active; });
// => objects for ['fred']
// The `_.matches` iteratee shorthand.
_.reject(users, { 'age': 40, 'active': true });
// => objects for ['barney']
// The `_.matchesProperty` iteratee shorthand.
_.reject(users, ['active', false]);
// => objects for ['fred']
// The `_.property` iteratee shorthand.
_.reject(users, 'active');
// => objects for ['barney']
Trả về tên toàn bộ những function của 1 object.
_.functions(object)
function Foo() {
this.a = _.constant('a');
this.b = _.constant('b');
}
Foo.prototype.c = _.constant('c');
_.functions(new Foo);
// => ['a', 'b']
Trả về toàn bộ key của object.
_.keys(object)
function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
_.keys(new Foo);
// => ['a', 'b'] (iteration order is not guaranteed)
_.keys('hi');
// => ['0', '1']
Chỉ lấy 1 số thuộc tính của object.
_.pick(object, [paths])
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pick(object, ['a', 'c']);
// => { 'a': 1, 'c': 3 }
Chỉ lấy 1 số thuộc tính của object theo điều kiện.
_.pickBy(object, [predicate=_.identity])
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pickBy(object, _.isNumber);
// => { 'a': 1, 'c': 3 }
Bỏ một số thuộc tính của object, lấy những thuộc tính còn lại.
_.omit(object, [paths])
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.omit(object, ['a', 'c']);
// => { 'b': '2' }
Bỏ một số thuộc tính của object theo điều kiện, lấy những thuộc tính còn lại.
_.omitBy(object, [predicate=_.identity])
var object = { 'a': 1, 'b': '2', 'c': 3 };
_.omitBy(object, _.isNumber);
// => { 'b': '2' }
Thay thế thành …(ký tự nào đó) bắt đầu từ đâu đến đâu (không tính end).
_.fill(array, value, [start=0], [end=array.length]) var array = [1, 2, 3]; _.fill(array, 'a'); console.log(array); // => ['a', 'a', 'a'] _.fill(Array(3), 2); // => [2, 2, 2] _.fill([4, 6, 8, 10], '*', 1, 3); // => [4, '*', '*', 10]
Tìm vị trí của item đầu tiên thỏa điều kiện.
_.findIndex(array, [predicate=_.identity], [fromIndex=0])
var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
_.findIndex(users, function(o) { return o.user == 'barney'; });
// => 0
// The `_.matches` iteratee shorthand.
_.findIndex(users, { 'user': 'fred', 'active': false });
// => 1
// The `_.matchesProperty` iteratee shorthand.
_.findIndex(users, ['active', false]);
// => 0
// The `_.property` iteratee shorthand.
_.findIndex(users, 'active');
// => 2
Tìm vị trí của item cuối cùng thỏa điều kiện.
_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1])
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': false }
];
_.findLastIndex(users, function(o) { return o.user == 'pebbles'; });
// => 2
// The `_.matches` iteratee shorthand.
_.findLastIndex(users, { 'user': 'barney', 'active': true });
// => 0
// The `_.matchesProperty` iteratee shorthand.
_.findLastIndex(users, ['active', false]);
// => 2
// The `_.property` iteratee shorthand.
_.findLastIndex(users, 'active');
// => 0
Search item in array, return index tìm thấy.
_.indexOf(array, value, [fromIndex=0]) _.indexOf([1, 2, 1, 2], 2); // => 1 // Search from the `fromIndex`. _.indexOf([1, 2, 1, 2], 2, 2); // => 3
Tìm và trả về key của một phần tử.
_.findKey(object, [predicate=_.identity])
var users = {
'barney': { 'age': 36, 'active': true },
'fred': { 'age': 40, 'active': false },
'pebbles': { 'age': 1, 'active': true }
};
_.findKey(users, function(o) { return o.age < 40; });
// => 'barney' (iteration order is not guaranteed)
// The `_.matches` iteratee shorthand.
_.findKey(users, { 'age': 1, 'active': true });
// => 'pebbles'
// The `_.matchesProperty` iteratee shorthand.
_.findKey(users, ['active', false]);
// => 'fred'
// The `_.property` iteratee shorthand.
_.findKey(users, 'active');
// => 'barney'
Hạ cấp mảng xuống 1 level.
_.flatten(array) _.flatten([1, [2, [3, [4]], 5]]); // => [1, 2, [3, [4]], 5]
Hạ cấp mảng về level 1.
_.flattenDeep(array) _.flattenDeep([1, [2, [3, [4]], 5]]); // => [1, 2, 3, 4, 5]
Hạ cấp mảng xuống n level.
_.flattenDepth(array, [depth=1]) var array = [1, [2, [3, [4]], 5]]; _.flattenDepth(array, 1); // => [1, 2, [3, [4]], 5] _.flattenDepth(array, 2); // => [1, 2, 3, [4], 5]
Tạo object từ [key, value] trong array.
_.fromPairs(pairs)
_.fromPairs([['a', 1], ['b', 2]]);
// => { 'a': 1, 'b': 2 }
Lấy array 1 làm key,
Lấy array 2 làm value.
_.zipObject(['a', 'b'], [1, 2]);
// => { 'a': 1, 'b': 2 }
Giống trên nhưng phức tạp hơn.
_.zipObjectDeep([props=[]], [values=[]])
_.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]);
// => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }
Tạo object mới ở địa chỉ mới, để khi thay đổi không ảnh hưởng object ban đầu.
_.cloneDeep(value)
var objects = [{ 'a': 1 }, { 'b': 2 }];
var deep = _.cloneDeep(objects);
console.log(deep[0] === objects[0]);
// => false
Đảo ngược phần tử của mảng.
_.reverse(array) var array = [1, 2, 3]; _.reverse(array); // => [3, 2, 1] console.log(array); // => [3, 2, 1]
Lấy ra những item duy nhất của một hoặc nhiều array.
_.union([arrays]) _.union([2], [1, 2]); // => [2, 1]
Lấy ra những item duy nhất của một hoặc nhiều array nhưng có thêm điều kiện.
_.unionBy([arrays], [iteratee=_.identity])
_.unionBy([2.1], [1.2, 2.3], Math.floor);
// => [2.1, 1.2]
// The `_.property` iteratee shorthand.
_.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]
Lấy ra những item duy nhất của một array.
_.uniq(array) _.uniq([2, 1, 2]); // => [2, 1]
Lấy ra những item duy nhất của một array nhưng theo điều kiện.
_.uniqBy(array, [iteratee=_.identity])
_.uniqBy([2.1, 1.2, 2.3], Math.floor);
// => [2.1, 1.2]
// The `_.property` iteratee shorthand.
_.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]
Xác định vị trí chèn vào mảng, có giá trị gần với giá trị cần chèn nhất và nhỏ hơn giá trị cần chèn.
_.sortedIndex(array, value)' _.sortedIndex([30, 50], 40); // => 1
Xác định vị trí chèn vào mảng, có giá trị gần với giá trị cần chèn nhất và nhỏ hơn giá trị cần chèn. Nhưng có thêm điều kiện.
_.sortedIndexBy(array, value, [iteratee=_.identity])
var objects = [{ 'x': 4 }, { 'x': 5 }];
_.sortedIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
// => 0
// The `_.property` iteratee shorthand.
_.sortedIndexBy(objects, { 'x': 4 }, 'x');
// => 0
Xác định vị trí chèn vào mảng đã được sắp xếp, có giá trị gần với giá trị cần chèn nhất và nhỏ hơn giá trị cần chèn.
_.sortedIndexOf(array, value) _.sortedIndexOf([4, 5, 5, 5, 6], 5); // => 1
Xác định vị trí chèn vào mảng, có giá trị gần với giá trị cần chèn nhất và lớn hơn giá trị cần chèn.
_.sortedLastIndex(array, value) _.sortedLastIndex([4, 5, 5, 5, 6], 5); // => 4
Xác định vị trí chèn vào mảng, có giá trị gần với giá trị cần chèn nhất và lớn hơn giá trị cần chèn. Nhưng có thêm điều kiện.
_.sortedLastIndexBy(array, value, [iteratee=_.identity])
var objects = [{ 'x': 4 }, { 'x': 5 }];
_.sortedLastIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
// => 1
// The `_.property` iteratee shorthand.
_.sortedLastIndexBy(objects, { 'x': 4 }, 'x');
// => 1
Sắp xếp mảng.
_.orderBy(collection, [iteratees=[_.identity]], [orders])
var users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 34 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 36 }
];
// Sort by `user` in ascending order and by `age` in descending order.
_.orderBy(users, ['user', 'age'], ['asc', 'desc']);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
Sắp xếp collection theo điều kiện.
_.sortBy(collection, [iteratees=[_.identity]])
var users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 34 }
];
_.sortBy(users, [function(o) { return o.user; }]);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
_.sortBy(users, ['user', 'age']);
// => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]
_.isNull(value) _.isNull(null); // => true _.isNull(void 0); // => false
_.isFunction(value) _.isFunction(_); // => true _.isFunction(/abc/); // => false
_.isUndefined(value) _.isUndefined(void 0); // => true _.isUndefined(null); // => false
_.isNumber(value)
_.isNumber(3);
// => true
_.isNumber(Number.MIN_VALUE);
// => true
_.isNumber(Infinity);
// => true
_.isNumber('3');
// => false
_.isObject(value)
_.isObject({});
// => true
_.isObject([1, 2, 3]);
// => true
_.isObject(_.noop);
// => true
_.isObject(null);
// => false
_.isDate(value)
_.isDate(new Date);
// => true
_.isDate('Mon April 23 2012');
// => false
_.isEmpty(value)
_.isEmpty(null);
// => true
_.isEmpty(true);
// => true
_.isEmpty(1);
// => true
_.isEmpty([1, 2, 3]);
// => false
_.isEmpty({ 'a': 1 });
// => false
Kiểm tra chuỗi có nằm ở đầu string cần so sánh.
_.startsWith([string=''], [target], [position=0])
_.startsWith('abc', 'a');
// => true
_.startsWith('abc', 'b');
// => false
_.startsWith('abc', 'b', 1);
// => true
Kiểm tra chuỗi có nằm ở cuối string cần so sánh.
_.endsWith([string=''], [target], [position=string.length])
_.endsWith('abc', 'c');
// => true
_.endsWith('abc', 'b');
// => false
_.endsWith('abc', 'b', 2);
// => true
Kiểm tra object có property hay function nào đó không.
_.has(object, path)
var object = { 'a': { 'b': 2 } };
var other = _.create({ 'a': _.create({ 'b': 2 }) });
_.has(object, 'a');
// => true
_.has(object, 'a.b');
// => true
_.has(object, ['a', 'b']);
// => true
_.has(other, 'a');
// => false
true => nếu tất cả phần tử thỏa điều kiện.
_.every(collection, [predicate=_.identity])
_.every([true, 1, null, 'yes'], Boolean);
// => false
var users = [
{ 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': false }
];
// The `_.matches` iteratee shorthand.
_.every(users, { 'user': 'barney', 'active': false });
// => false
// The `_.matchesProperty` iteratee shorthand.
_.every(users, ['active', false]);
// => true
// The `_.property` iteratee shorthand.
_.every(users, 'active');
// => false
true => nếu một trong các phần tử thỏa điều kiện.
_.some(collection, [predicate=_.identity])
_.some([null, 0, 'yes', false], Boolean);
// => true
var users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false }
];
// The `_.matches` iteratee shorthand.
_.some(users, { 'user': 'barney', 'active': false });
// => false
// The `_.matchesProperty` iteratee shorthand.
_.some(users, ['active', false]);
// => true
// The `_.property` iteratee shorthand.
_.some(users, 'active');
// => true
Kiểm tra giá trị có nằm trong collection không.
_.includes(collection, value, [fromIndex=0])
_.includes([1, 2, 3], 1);
// => true
_.includes([1, 2, 3], 1, 2);
// => false
_.includes({ 'a': 1, 'b': 2 }, 1);
// => true
_.includes('abcd', 'bc');
// => true
Kiểm tra có giống nhau không.
_.isEqual(value, other)
var object = { 'a': 1 };
var other = { 'a': 1 };
_.isEqual(object, other);
// => true
object === other;
// => false
Chuyển chữ dạng camel case.
_.camelCase([string=''])
_.camelCase('Foo Bar');
// => 'fooBar'
_.camelCase('--foo-bar--');
// => 'fooBar'
_.camelCase('__FOO_BAR__');
// => 'fooBar'
Viết hoa chữ cái đầu.
_.capitalize([string=''])
_.capitalize('FRED');
// => 'Fred'
Bổ sung chữ bằng cách thêm ký tự 2 bên.
_.pad([string=''], [length=0], [chars=' '])
_.pad('abc', 8);
// => ' abc '
_.pad('abc', 8, '_-');
// => '_-abc_-_'
_.pad('abc', 3);
// => 'abc'
Bổ sung chữ bằng cách thêm ký tự bên trái.
_.padStart([string=''], [length=0], [chars=' '])
_.padStart('abc', 6);
// => ' abc'
_.padStart('abc', 6, '_-');
// => '_-_abc'
_.padStart('abc', 3);
// => 'abc'
Bổ sung chữ bằng cách thêm ký tự bên phải.
_.padEnd([string=''], [length=0], [chars=' '])
_.padEnd('abc', 6);
// => 'abc '
_.padEnd('abc', 6, '_-');
// => 'abc_-_'
_.padEnd('abc', 3);
// => 'abc'
Viết hoa chữ cái đầu mỗi chữ.
_.startCase([string=''])
_.startCase('--foo-bar--');
// => 'Foo Bar'
_.startCase('fooBar');
// => 'Foo Bar'
_.startCase('__FOO_BAR__');
// => 'FOO BAR'
Chuyển chữ có dấu thành chữ không dấu.
_.deburr([string=''])
_.deburr('déjà vu');
// => 'deja vu'
Lọc ra item theo điều kiện.
_.filter(collection, [predicate=_.identity])
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
_.filter(users, function(o) { return !o.active; });
// => objects for ['fred']
// The `_.matches` iteratee shorthand.
_.filter(users, { 'age': 36, 'active': true });
// => objects for ['barney']
// The `_.matchesProperty` iteratee shorthand.
_.filter(users, ['active', false]);
// => objects for ['fred']
// The `_.property` iteratee shorthand.
_.filter(users, 'active');
// => objects for ['barney']
Lọc ra item theo điều kiện. Tương tự hàm _.filter.
_.find(collection, [predicate=_.identity], [fromIndex=0])
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }
];
_.find(users, function(o) { return o.age < 40; });
// => object for 'barney'
// The `_.matches` iteratee shorthand.
_.find(users, { 'age': 1, 'active': true });
// => object for 'pebbles'
// The `_.matchesProperty` iteratee shorthand.
_.find(users, ['active', false]);
// => object for 'fred'
// The `_.property` iteratee shorthand.
_.find(users, 'active');
// => object for 'barney'
Lọc giống như filter nhưng trả về dạng object group_by.
Lưu ý: Cần return và đối số thứ ba là {}, là kết quả mặc định khi không có dữ liệu.
_.reduce(collection, [iteratee=_.identity], [accumulator])
_.reduce([1, 2], function(sum, n) {
return sum + n;
}, 0);
// => 3
_.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
(result[value] || (result[value] = [])).push(key);
return result;
}, {});
// => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)
Thực hiện số lần cụ thể cho function và trả về array kết quả.
_.times(n, [iteratee=_.identity]) _.times(3, String); // => ['0', '1', '2'] _.times(4, _.constant(0)); // => [0, 0, 0, 0]
Sau một khoảng thời gian, chỉ chạy một lần. Giống setTimeout.
_.delay(func, wait, [args])
_.delay(function(text) {
console.log(text);
}, 1000, 'later');
// => Logs 'later' after one second.
Gọi một function sau một khoảng thời gian function đó được gọi. Giống setIntervel
_.debounce(func, [wait=0], [options={}])
// Avoid costly calculations while the window size is in flux.
jQuery(window).on('resize', _.debounce(calculateLayout, 150));
// Invoke `sendMail` when clicked, debouncing subsequent calls.
jQuery(element).on('click', _.debounce(sendMail, 300, {
'leading': true,
'trailing': false
}));
// Ensure `batchLog` is invoked once after 1 second of debounced calls.
var debounced = _.debounce(batchLog, 250, { 'maxWait': 1000 });
var source = new EventSource('/stream');
jQuery(source).on('message', debounced);
// Cancel the trailing debounced invocation.
jQuery(window).on('popstate', debounced.cancel);
Lấy dữ liệu mà không bị lỗi undefined.
_.get(object, path, [defaultValue])
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.get(object, 'a[0].b.c');
// => 3
_.get(object, ['a', '0', 'b', 'c']);
// => 3
_.get(object, 'a.b.c', 'default');
// => 'default'
Gán dữ liệu mà không bị lỗi undefined.
_.set(object, path, value)
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.set(object, 'a[0].b.c', 4);
console.log(object.a[0].b.c);
// => 4
_.set(object, ['x', '0', 'y', 'z'], 5);
console.log(object.x[0].y.z);
// => 5
Tạo ra mảng mới sau khi xử lý cái gì đó ở mảng cũ.
_.map(collection, [iteratee=_.identity])
function square(n) {
return n * n;
}
_.map([4, 8], square);
// => [16, 64]
_.map({ 'a': 4, 'b': 8 }, square);
// => [16, 64] (iteration order is not guaranteed)
var users = [
{ 'user': 'barney' },
{ 'user': 'fred' }
];
// The `_.property` iteratee shorthand.
_.map(users, 'user');
// => ['barney', 'fred']
Trả về object mới từ object cũ sau khi đã xử lý gì đó.
_.mapValues(object, [iteratee=_.identity])
var users = {
'fred': { 'user': 'fred', 'age': 40 },
'pebbles': { 'user': 'pebbles', 'age': 1 }
};
_.mapValues(users, function(o) { return o.age; });
// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
// The `_.property` iteratee shorthand.
_.mapValues(users, 'age');
// => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
Lặp qua các element.
_.forEach(collection, [iteratee=_.identity])
_.forEach([1, 2], function(value) {
console.log(value);
});
// => Logs `1` then `2`.
_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
console.log(key);
});
// => Logs 'a' then 'b' (iteration order is not guaranteed).
Gom nhóm object.
_.groupBy(collection, [iteratee=_.identity])
_.groupBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': [4.2], '6': [6.1, 6.3] }
// The `_.property` iteratee shorthand.
_.groupBy(['one', 'two', 'three'], 'length');
// => { '3': ['one', 'two'], '5': ['three'] }
Trả về item lớn nhất trong array.
_.max(array) _.max([4, 2, 8, 6]); // => 8 _.max([]); // => undefined
Làm tròn số lên.
5.1 ==> 6
5.6 ==> 6
_.ceil(number, [precision=0]) _.ceil(4.006); // => 5 _.ceil(6.004, 2); // => 6.01 _.ceil(6040, -2); // => 6100
Làm tròn số xuống.
5.1 ==> 5
5.6 ==> 5
_.floor(number, [precision=0]) _.floor(4.006); // => 4 _.floor(0.046, 2); // => 0.04 _.floor(4060, -2); // => 4000
Làm tròn số theo cách thông thường. > 5 lên. < 5 xuống.
5.1 ==> 5
5.6 ==> 6
_.round(number, [precision=0]) _.round(4.006); // => 4 _.round(4.006, 2); // => 4.01 _.round(4060, -2); // => 4100