說(shuō)到遍歷案怯,首先想到的是數(shù)組的遍歷君旦,方法不要太多,比如 for, forEach于宙,map浮驳,filter悍汛,every捞魁,some等
下面來(lái)看下,用法 首先 定義一個(gè)數(shù)組:?
1. for循環(huán)离咐,需要知道數(shù)組的長(zhǎng)度谱俭,才能遍歷,
2. forEach循環(huán)宵蛀,循環(huán)數(shù)組中每一個(gè)元素并采取操作昆著, 沒(méi)有返回值, 可以不用知道數(shù)組長(zhǎng)度
3. map函數(shù)术陶,遍歷數(shù)組每個(gè)元素凑懂,并回調(diào)操作,需要返回值梧宫,返回值組成新的數(shù)組接谨,原數(shù)組不變
4. filter函數(shù), 過(guò)濾通過(guò)條件的元素組成一個(gè)新數(shù)組塘匣, 原數(shù)組不變
5. some函數(shù)脓豪,遍歷數(shù)組中是否有符合條件的元素,返回Boolean值
6. every函數(shù)忌卤, 遍歷數(shù)組中是否每個(gè)元素都符合條件扫夜, 返回Boolean值
當(dāng)然, 除了遍歷數(shù)組之外驰徊,還有遍歷對(duì)象笤闯,常用方法 in
in 不僅可以用來(lái) 遍歷對(duì)象,還可以用來(lái)遍歷數(shù)組棍厂, 不過(guò) i 對(duì)應(yīng)與數(shù)組的 key值
介紹完遍歷颗味,下面說(shuō)一下工作中遇到的情況,后臺(tái)傳給我一個(gè)對(duì)象數(shù)組勋桶,我需要排序再顯示脱衙,看到有介紹用 sort 排序的方法,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var?arr1 = [
????{name:?'te', value: 5},
????{name:?'te', value: 2},
????{name:?'we', value: 3},
????{name:?'ee', value: 1},
????{name:?'ee', value: 4}
];
var?by =?function(type){
????return?function(o,p){
????????console.log(o,p);
????????var?a;
????????var?b;
????????if(typeof?o ===?'object'?&&?typeof?p ===?'object'?&& o && p){
????????????a = o[type];
????????????b = p[type];
????????????if(a === b) {
????????????????return?0;
????????????}
????????????if(typeof?a ===?typeof?b){
????????????console.log(a, b);
????????????????return?a < b ? -1 : 1
????????????}
????????????return?typeof?a <?typeof?b ? -1 : 1;
????????}else?{
????????????throw('字段有誤');
????????}
????}
}
console.log(arr1.sort(by('value')));
顯示如下:
總結(jié):
排序應(yīng)用場(chǎng)景很多 例驹, 所以要弄清楚捐韩,明白,方可游刃有余鹃锈。望小伙伴多多提意見(jiàn)荤胁!
補(bǔ)充: ?后面發(fā)現(xiàn), 后臺(tái)傳過(guò)來(lái)的數(shù)組中屎债,每個(gè)對(duì)象按 value 排序, value > 5的按順序排在前面仅政,小于5排在后面
思考后垢油, 可以在原來(lái)的的方法中這樣寫(xiě),將數(shù)組分成2段,大于等于5和小于5圆丹,交換位置即可
var?arr1 = [
????{name:?'te', value: 5},
????{name:?'te', value: 2},
????{name:?'we', value: 3},
????{name:?'ee', value: 1},
????{name:?'ee', value: 4}
];
var?sortObj =?function(arr, type , num){
????var?by =?function(type){
????????return?function(o,p){
????????????var?a;
????????????var?b;
????????????if(typeof?o ===?'object'?&&?typeof?p ===?'object'?&& o && p){
????????????????a = o[type];
????????????????b = p[type];
????????????????if(a === b) {
????????????????????return?0;
????????????????}
????????????????if(typeof?a ===?typeof?b){
????????????????console.log(a, b);
????????????????????return?a < b ? -1 : 1
????????????????}
????????????????return?typeof?a <?typeof?b ? -1 : 1;
????????????}else?{
????????????????throw('字段有誤');
????????????}
????????}
????};
????var?cacheArr = arr.sort(by('value'));
????//通過(guò)num 把數(shù)組分成兩段
????var?arrBf = cacheArr.filter(function(item){
????????if(item.value < num){
????????????return?item;
????????}
????});
????var?arrAf = cacheArr.filter(function(item){
????????if(item.value >= num){
????????????return?item;
????????}
????});
????//交換位置 即可得到
????var?newArr = arrAf.concat(arrBf);
????return?newArr;
};
console.log(sortObj(arr1,?'value'?, 3));