Radash 数组工具函数
新一代的工具库,里面封装了大量的工具函数,方便开发使用
安装
NPM
bash
npm install radash
YARN
bash
yarn add radash
数组工具函数
boil(排序)
自定义排序规则
- 基础用法
给定一个项目数组,返回匹配条件的最终项,适合更复杂的最小/最大值
js
const testarr = () => {
const gods = [
{ name: "Ra", power: "sun", rank: 100, culture: "egypt" },
{ name: "Loki", power: "tricks", rank: 72, culture: "norse" },
{ name: "Zeus", power: "lightning", rank: 106, culture: "greek" },
];
// 自定义排序规则,设定规则要是大于就返回a,要是小于就返回b
console.log(
_.boil(gods, (a, b) => {
if (a.rank > b.rank) {
return a;
} else {
return b;
}
})
);
};
// 最后的结果 { name: "Zeus", power: "lightning", rank: 106, culture: "greek" },
拆分数组
- 基础用法
将列表拆分成给定大小的多个列表
js
const splitarr = () => {
const gods = [
"Ra",
"Zeus",
"Loki",
"Vishnu",
"Icarus",
"Osiris",
"Thor",
"Apollo",
"Artemis",
"Athena",
];
// 切割数组
console.log(_.cluster(gods, 3));
// 结果
/*
[
[ 'Ra', 'Zeus', 'Loki' ],
[ 'Vishnu', 'Icarus', 'Osiris' ],
['Thor', 'Apollo', 'Artemis'],
['Athena']
]
*/
};
计数
- 基础用法
计算这个属性在数组中出现的次数
js
const testcounting = () => {
const gods = [
{
name: "Ra",
culture: "egypt",
},
{
name: "Zeus",
culture: "greek",
},
{
name: "Loki",
culture: "greek",
},
];
console.log(_.counting(gods, (g) => g.culture));
// { egypt: 1, greek: 2 }
};
diff
- 基础用法
找到第一个数组中不在第二个数组的元素
js
const testdiff = () => {
const oldWorldGods = ["ra", "zeus"];
const newWorldGods = ["vishnu", "zeus"];
console.log(_.diff(oldWorldGods, newWorldGods));
// [ 'ra' ]
};
first
- 基础用法
返回数组中的第一个元素
js
// 获取第一个
const testfirst = () => {
const gods = ["ra", "zeus", "loki"];
console.log(_.first(gods));
};
// ra
last
- 基础用法
返回数组中的最后一个元素
js
const testlast = () => {
const gods = ["ra", "zeus", "loki"];
console.log(_.last(gods));
};
// loki
max
- 基础用法
返回数组中的最大值,如果有重复的,则返回最后一个
js
// max 最大值
const testmax = () => {
const gods = [
{
name: "Ra",
power: 100,
},
{
name: "Zeus",
power: 98,
},
{
name: "Loki",
power: 72,
},
{
name: "Vishnu",
power: 100,
},
];
console.log(
_.max(gods, (item) => {
item.power;
})
);
};
/*
{
"name": "Vishnu",
"power": 100
}
*/
min
- 基础用法
返回数组中的最小值
js
const testmin = () => {
const gods = [
{
name: "Ra",
power: 100,
},
{
name: "Zeus",
power: 98,
},
{
name: "Loki",
power: 72,
},
{
name: "Vishnu",
power: 100,
},
];
console.log(_.min(gods, (item) => item.power));
};
/*
{
name: "Loki",
power: 72,
},
*/
flat
- 基础用法
将数组扁平化一维数组,二维数组变成一维数组
js
const testflat = () => {
const gods = [["ra"], ["zeus", "loki"], ["vishnu"]];
console.log(_.flat(gods));
// [ 'ra', 'zeus', 'loki', 'vishnu' ]
};
fork
- 基础用法
按照条件将数组分割成两个数组
js
const customsplitarr = () => {
const gods = [
{
name: "Ra",
power: 100,
},
{
name: "Zeus",
power: 98,
},
{
name: "Loki",
power: 72,
},
{
name: "Vishnu",
power: 100,
},
];
let result = _.fork(gods, (item) => item.power > 80);
console.log(result);
/*
[
[
{
"name": "Ra",
"power": 100
},
{
"name": "Zeus",
"power": 98
},
{
"name": "Vishnu",
"power": 100
}
],
[
{
"name": "Loki",
"power": 72
}
]
]
*/
};
group
- 基础用法
按照属性的值来分组
js
const testgroup = () => {
const gods = [
{
name: "Ra",
power: 100,
},
{
name: "Zeus",
power: 98,
},
{
name: "Loki",
power: 72,
},
{
name: "Loki",
power: 84,
},
{
name: "Vishnu",
power: 100,
},
];
const result = _.group(gods, (item) => item.power);
console.log(result);
};
/*
{
"72": [
{
"name": "Loki",
"power": 72
}
],
"84": [
{
"name": "Loki",
"power": 84
}
],
"98": [
{
"name": "Zeus",
"power": 98
}
],
"100": [
{
"name": "Ra",
"power": 100
},
{
"name": "Vishnu",
"power": 100
}
]
}
*/
求交集 intersects
- 基础用法
求交集,只要有交集 就返回 true,没交集就返回 false, 下面这个返回 true
js
const testintersect = () => {
const oldWorldGods = ["ra", "zeus"];
const newWorldGods = ["vishnu", "zeus"];
console.log(_.intersects(oldWorldGods, newWorldGods));
};
// true
求和 iterate
- 基础用法
求和,第一个代表次数,第二个是执行的函数,第三个是初始值,特别注意 idx 从 1 开始
js
const testiterate = () => {
console.log(
_.iterate(
4,
(acc, idx) => {
return acc + 1;
},
10
)
);
};
// 结果14
merge
- 基础用法
merge 合并数组,假如第一个原数组,第二个要合并的数组,第三个是合并的规则,如果 key 相同,则合并,否则不合并
不写第三个条件,则合并不了,返回原数组
js
const testmerge = () => {
const gods = [
{
name: "Zeus",
power: 98,
},
{
name: "Ra",
power: 68,
},
{
name: "Ra333",
power: 685,
},
];
const newGods = [
{
name: "Zeus",
power: 128,
},
{
name: "Ra",
power: 685,
},
];
console.log(_.merge(gods, newGods, (f) => f.name));
};
/*
[
{
"name": "Zeus",
"power": 128
},
{
"name": "Ra",
"power": 685
},
{
"name": "Ra333",
"power": 685
}
]
*/
objectify
- 基础用法
将列表转化为对象
js
const testobj2list = () => {
const fish = [
{
name: "Marlin",
weight: 105,
},
{
name: "Bass",
weight: 8,
},
{
name: "Trout",
weight: 13,
},
];
console.log(_.objectify(fish, (item) => item.name));
};
/*
{
"Marlin": {
"name": "Marlin",
"weight": 105
},
"Bass": {
"name": "Bass",
"weight": 8
},
"Trout": {
"name": "Trout",
"weight": 13
}
}
*/
replacOradd
基础用法
替换或者追加 其中项目要么替换现有项目的索引 - 如果存在,否则将其附加到末尾。 末尾条件是替换的条件
js
const replacOradd = () => {
const list1 = [
{
name: "Marlin",
weight: 105,
},
{
name: "Salmon45",
weight: 19,
},
{
name: "Trout",
weight: 13,
},
];
const obj1 = {
name: "替换的结果",
weight: 219,
};
console.log(_.replaceOrAppend(list1, obj1, (item) => item.name === "Salmon"));
};
/*
[
{
"name": "Marlin",
"weight": 105
},
{
"name": "Salmon45",
"weight": 19
},
{
"name": "Trout",
"weight": 13
},
{
"name": "替换的结果",
"weight": 219
}
]
*/
replace
基础用法
仅仅替换不会添加
js
const testreplace = () => {
const fish = [
{
name: "Marlin",
weight: 105,
},
{
name: "Bass",
weight: 8,
},
{
name: "Trout",
weight: 13,
},
];
console.log(
_.replace(
fish,
{ name: "要替换的", weight: 18 },
(item) => item.name === "Bass"
)
);
};
/*
[
{
"name": "Marlin",
"weight": 105
},
{
"name": "替换的新内容",
"weight": 18
},
{
"name": "Trout",
"weight": 13
}
]
*/
select
- 基础用法
过滤和映射数组,第一个参数数组,第二个参数过滤,第三个参数找的属性
js
const testfilterarr = () => {
const fish = [
{
name: "Marlin",
weight: 105,
source: "ocean",
},
{
name: "Bass",
weight: 8,
source: "lake",
},
{
name: "Trout",
weight: 13,
source: "lake",
},
];
console.log(
_.select(
fish,
(f) => f.weight,
(f) => f.source === "lake"
)
);
};
/*
[
8,
13
]
sift
- 基础用法
筛选,只返回不为假的项目
js
const testsift = () => {
const fish = ["salmon", null, false, NaN, "sockeye", "bass"];
_.sift(fish); // => ['salmon', 'sockeye', 'bass']
console.log(_.sift(fish));
};
sort
- 基础用法
排序,默认是升序,如果要是加第三个参数 就是降序
js
const testsort = () => {
const fish = [
{
name: "Marlin",
weight: 105,
},
{
name: "Bass",
weight: 8,
},
{
name: "Trout",
weight: 13,
},
];
console.log(_.sort(fish, (f) => f.weight, true));
/*
[
{
"name": "Marlin",
"weight": 105
},
{
"name": "Trout",
"weight": 13
},
{
"name": "Bass",
"weight": 8
}
]
*/
};
sum
- 基础用法
求和
js
const testsum = () => {
const fish = [
{
name: "Marlin",
weight: 105,
},
{
name: "Bass",
weight: 8,
},
{
name: "Trout",
weight: 13,
},
];
console.log(_.sum(fish, (f) => f.weight));
// 126
};
unique
- 基础用法
依据某个属性去重,如果有重复,保留第一个
js
const testunique = () => {
const fish = [
{
name: "Marlin",
weight: 105,
source: "ocean",
},
{
name: "Salmon",
weight: 22,
source: "river",
},
{
name: "Salmon",
weight: 252,
source: "river",
},
];
console.log(_.unique(fish, (f) => f.name));
/*
[
{
"name": "Marlin",
"weight": 105,
"source": "ocean"
},
{
"name": "Salmon",
"weight": 22,
"source": "river"
}
]
*/
};