Skip to content

Array 扩展说明

YHH edited this page Aug 11, 2020 · 4 revisions

findIndex

用于获取满足表达式的数组元素索引

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

any

用于判断是否存在满足表达式的数组元素

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

firstOrDefault

获取满足表达式的第一个或默认数组元素 如果找不到则返回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"}

find

获取满足表达式的第一个数组元素 等同于firstOrDefault

where

筛选满足表达式的数组元素

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}]

count

获取满足表达式的数组元素的计数

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

findAll

获取满足表达式的数组元素的数组 等同于 where

contains

是否有获取满足表达式的数组元素

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

removeAll

移除满足表达式的数组元素

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}]

remove

移除数组元素

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}]

removeAt

移除特定索引数组元素

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}]

removeRange

移除范围数组元素

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}]

select

获取通过选择器转换的数组

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]

orderBy

排序(升序)

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}]

orderByDescending

排序(降序)

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}]

groupBy

分组

let array = [6.1, 4.2, 6.3];

let data = array.groupBy(Math.floor);

data 输出 {"4": [4.2], "6": [6.1, 6.3]}

sum

求和

let array = [6.1, 4.2, 6.3];

let data = array.sum(Math.floor);

data 输出 16