-
-
Notifications
You must be signed in to change notification settings - Fork 77
Array 扩展说明
用于获取满足表达式的数组元素索引
let array = [
{id: 2, name: "json"},
{id: 1, name: "john"},
{id: 4, name: "jom"}
];
// 寻找id等于1的位置
let index = array.findIndex(a => {return a.id == 1;});
index输出
1
用于判断是否存在满足表达式的数组元素
let array = [
{id: 2, name: "json"},
{id: 1, name: "john"},
{id: 4, name: "jom"}
];
// 判断数组中是否存在id等于1值
let exist = array.any(a => {return a.id == 1;});
exist输出
true
获取满足表达式的第一个或默认数组元素 如果找不到则返回null
let array = [
{id: 2, name: "json"},
{id: 1, name: "john"},
{id: 4, name: "jom"}
];
let data = array.firstOrDefault(a => {return a.id == 1;});
data 输出
{id: 1, name: "john"}
获取满足表达式的第一个数组元素 等同于firstOrDefault
筛选满足表达式的数组元素
let array = [
{id: 2, name: "json"},
{id: 1, name: "john"},
{id: 4, name: "jom"},
{id: 5, name: "jom"}
];
let data = array.where(a => {return a.name == "jom";});
data 输出
[{id: 4, name: "jom"}, {id: 5, name: "jom}]
获取满足表达式的数组元素的计数
let array = [
{id: 2, name: "json", score: 90},
{id: 1, name: "john", score: 100},
{id: 4, name: "jom", score: 60},
{id: 5, name: "jom", score: 10}
];
let data = array.count(a => {return a.score;});
data 输出
260
获取满足表达式的数组元素的数组 等同于 where
是否有获取满足表达式的数组元素
let array = [
{id: 2, name: "json", score: 90},
{id: 1, name: "john", score: 100},
{id: 4, name: "jom", score: 60},
{id: 5, name: "jom", score: 10}
];
let data = array.contains(array[0]);
data 输出
true
移除满足表达式的数组元素
let array = [
{id: 2, name: "json", score: 90},
{id: 1, name: "john", score: 100},
{id: 4, name: "jom", score: 60},
{id: 5, name: "jom", score: 10}
];
array.removeAll(a => {return a.name == "jom";});
array 输出
[{id: 2, name: "json", score: 90},{id: 1, name: "john", score: 100}]
移除数组元素
let array = [
{id: 2, name: "json", score: 90},
{id: 1, name: "john", score: 100},
{id: 4, name: "jom", score: 60},
{id: 5, name: "jom", score: 10}
];
let result = array.remove(array[2]);
result 输出
true
array 输出[{id: 2, name: "json", score: 90},{id: 1, name: "john", score: 100},{id: 5, name: "jom", score: 10}]
移除特定索引数组元素
let array = [
{id: 2, name: "json", score: 90},
{id: 1, name: "john", score: 100},
{id: 4, name: "jom", score: 60},
{id: 5, name: "jom", score: 10}
];
array.removeAt(2);
array 输出
[{id: 2, name: "json", score: 90},{id: 1, name: "john", score: 100},{id: 5, name: "jom", score: 10}]
移除范围数组元素
let array = [
{id: 2, name: "json", score: 90},
{id: 1, name: "john", score: 100},
{id: 4, name: "jom", score: 60},
{id: 5, name: "jom", score: 10}
];
array.removeRange(2, 1);
array 输出
[{id: 2, name: "json", score: 90},{id: 1, name: "john", score: 100},{id: 5, name: "jom", score: 10}]
获取通过选择器转换的数组
let array = [
{id: 2, name: "json", score: 90},
{id: 1, name: "john", score: 100},
{id: 4, name: "jom", score: 60},
{id: 5, name: "jom", score: 10}
];
let data = array.select(a => {return a.id;});
data 输出
[2, 1, 4, 5]
排序(升序)
let array = [
{id: 2, name: "json", score: 90},
{id: 1, name: "john", score: 100},
{id: 4, name: "jom", score: 60},
{id: 5, name: "jom", score: 10}
];
let data = array.orderBy(a => {return a.id;});
data 输出
[{id: 1, name: "john", score: 100},{id: 2, name: "json", score: 90},{id: 4, name: "jom", score: 60},{id: 5, name: "jom", score: 10}]
排序(降序)
let array = [
{id: 2, name: "json", score: 90},
{id: 1, name: "john", score: 100},
{id: 4, name: "jom", score: 60},
{id: 5, name: "jom", score: 10}
];
let data = array.orderByDescending(a => {return a.id;});
data 输出
[{id: 5, name: "jom", score: 10},{id: 4, name: "jom", score: 60},{id: 2, name: "json", score: 90},{id: 1, name: "john", score: 100}]
分组
let array = [6.1, 4.2, 6.3];
let data = array.groupBy(Math.floor);
data 输出
{"4": [4.2], "6": [6.1, 6.3]}
求和
let array = [6.1, 4.2, 6.3];
let data = array.sum(Math.floor);
data 输出
16