Lodash是一个JavaScript库,帮助处理数组,集合,字符串,对象,数字等。
filter
范例1:function()
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'user':'luv',
'salary':36000,
'active':true },
{ 'user':'kush',
'salary':40000,
'active':false }
];
// Using the _.filter() method
let filtered_array = _.filter(
users, function(o) {
return !o.active;
}
);
// Printing the output
console.log(filtered_array);
输出:
[ { user:'kush', salary:40000, active:false } ]
范例2:{}
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'user':'luv',
'salary':36000,
'active':true },
{ 'user':'kush',
'salary':40000,
'active':false }
];
// Using the _.filter() method
// The `_.matches` iteratee shorthand
let filtered_array = _.filter(users,
{ 'salary':36000, 'active':true }
);
// Printing the output
console.log(filtered_array);
输出:
[ { user:'kush', salary:40000, active:false } ]
范例3:[]
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'user':'luv',
'salary':36000,
'active':true },
{ 'user':'kush',
'salary':40000,
'active':false }
];
// Using the _.filter() method
// The `_.matchesProperty` iteratee shorthand
let filtered_array =
_.filter(users, ['active', false]);
// Printing the output
console.log(filtered_array);
输出:
[ { user:'kush', salary:40000, active:false } ]
范例4:boolean类型
// Requiring the lodash library
const _ = require("lodash");
// Original array
var users = [
{ 'user':'luv',
'salary':36000,
'active':true },
{ 'user':'kush',
'salary':40000,
'active':false }
];
// Using the _.filter() method
// The `_.property` iteratee shorthand
let filtered_array =
_.filter(users, 'active');
// Printing the output
console.log(filtered_array);
输出:
[ { user:'luv', salary:36000, active:true } ]
map
1. 获取对象中某个字段的集合 pluck or map
var collection = [
{name: '', age:45}
];
_.pluck(collection, 'age'); // [45]
// 也可以这样写:
_.map(collection, 'age'); // [45]
2. 获取想要的字段们, 多个 pick
const collection = [
{ name: '', age: 45 },
{ name: 'tt', age: 23, hua: 'hua' },
];
const tt = _.map(collection, (item) => {
return _.pick(item, ['name', 'age']);
});
console.log(tt); // [ {name: "", age: 45}, {name: "tt", age: 23} ]
3. 排除不想要的字段们,不想要的字段作为参数传递 omit
const collection = [
{ name: '', age: 45 },
{ name: 'tt', age: 23, hua: 'hua' },
];
const tt = _.map(collection, (item) => {
return _.omit(item, ['name']);
});
console.log(tt); // [ {age: 45}, {age: 23, hua: "hua"} ]
4. map方法使用内置函数,计算每一个集合元素的大小 size
var collection=[
[1,2],
[1,2,3],
{first:1, second:2},
{first:1, second:2,third:3}
];
var result = _.map(collection, _.size);
//[ 2, 3, 2, 3 ]
console.log(result);
5. map方法使用内置函数,获取每个集合元素中的最小值 min
const collection=[
[2, 4, 5, 7, 8, 9],
[12, 1, 55, 0, 38, 9],
[22, 43, 15, 17, 28, 9],
];
console.log(collection);
// [2, 0, 9]
console.log(_.map(collection, _.min));
6. map方法使用内置函数,针对集合元素依次调用多个方法 flowRight first
// flowRight方法会依次调用其参数方法
// 先给每个一个元素内的元素排序
// 然后取出元素中的元素中的第一个
var collection = [
[ 'Evan', 'Veronica', 'Dana' ],
[ 'Lila', 'Ronald', 'Dwayne' ],
[ 'Ivan', 'Alfred', 'Doug' ],
[ 'Penny', 'Lynne', 'Andy' ]
];
var result = _.map(collection, _.flowRight(_.first, function(item){
return _.sortBy(item);
}));
//[ 'Dana', 'Dwayne', 'Alfred', 'Andy' ]
console.log(result);
compact
compact()函数用于创建一个数组,其中删除了JavaScript中所有错误值。
注意:值false,null,0,“”,undefined和NaN为falsey。
flatMap
flatMap
函数可以将一个数组中的每个元素映射为一个新数组,并将所有的新数组合并为一个数组。
const _ = require('lodash');
const data = [{servers:[1,2,3], otther:'111'},{servers:[3,4,5], otther:'222'}];
const result = _.flatMap(data, 'servers');
console.log(result);
输出结果为:
[1, 2, 3, 3, 4, 5]
在这个示例中,_.flatMap(data, 'servers')
表示对data
数组中的每个对象的servers
属性进行映射,并将所有的映射结果合并为一个数组。
xorBy
xorBy
是lodash
库中的一个方法,用于计算两个数组的对称差异(symmetric difference)。它根据指定的迭代函数对数组进行比较,并返回在其中一个数组中出现但不在另一个数组中出现的元素。
xorBy
方法接受三个参数:array1
,array2
和iteratee
。array1
和array2
是要比较的两个数组,而iteratee
是一个函数,用于将元素转换为比较用的值。
import { xorBy } from 'lodash';
const array1 = [1.2, 2.4];
const array2 = [2.6, 3.8];
const iteratee = Math.floor; // Math.floor是JavaScript中的一个内置函数,用于将一个数值向下取整,返回小于或等于给定数值的最大整数。
const result = xorBy(array1, array2, iteratee);
console.log(result); // [1.2, 3.8]