JavaScript
Array
2019/5/22陳宏彰
排序
sort()
var a = [5,3,2,4,2,1,3,4,5,2,3,4,1,2,3];
console.log(a);
//[5, 3, 2, 4, 2, 1, 3, 4, 5, 2, 3, 4, 1, 2, 3]
a.sort();
console.log(a);
//[1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5]
var a = [5,3,2,4,2,1,3,4,5,2,3,4,1,2,3];
console.log(a);
//[5, 3, 2, 4, 2, 1, 3, 4, 5, 2, 3, 4, 1, 2, 3]
//函數定義
function b(a,b){
if(a < b) return 1; //回傳 1 代表 a 要排在 b 前面
else if(a > b) return -1; //回傳 -1 代表 b 要排在 a 前面
else return 0; //回傳 0 代表不交換
}
a.sort(b); //用函數當參數傳入
console.log(a);
//[5, 5, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1]
Try it !
奇數排前面,偶數排後面
var a = [5,3,2,4,2,1,3,4,5,2,3,4,1,2,3];
function f(a,b){
if(a%2 == 1 && b%2 == 0) return -1;
if(a%2 == 0 && b%2 == 1) return 1;
return 0;
}
a.sort(f);
console.log(a);
//[5, 3, 1, 3, 5, 3, 1, 3, 2, 4, 2, 4, 2, 4, 2]
Try it !
奇數排前面,偶數排後面,相同者小的排前面
var a = [5,3,2,4,2,1,3,4,5,2,3,4,1,2,3];
function f(a,b){
if(a%2 == 1 && b%2 == 0) return -1;
if(a%2 == 0 && b%2 == 1) return 1;
if(a > b) return 1;
if(a < b) return -1;
return 0;
}
a.sort(f);
console.log(a);
//[1, 1, 3, 3, 3, 3, 5, 5, 2, 2, 2, 2, 4, 4, 4]
Try it !
依照除以11的餘數大小排序(小到大),餘數相同者小的排前面
var a = [65,367,51,236,54,9,563,2,5,42,3,552,4,649,746,326,9895,66,54];
var a = [65,367,51,236,54,9,563,2,5,42,3,552,4,649,746,326,9895,66,54];
function f(a,b){
if(a%11 > b%11) return 1;
if(a%11 < b%11) return -1;
if(a%11 == b%11){
if(a > b) return 1;
if(a < b) return -1;
return 0;
}
}
a.sort(f);
console.log(a);
//[66, 649, 2, 552, 563, 3, 4, 367, 5, 236, 9895, 51, 326, 9, 42, 746, 54, 54, 65]
// 0 2 3 4 5 6 7 9 10
過濾
filter()
var a = [1,54,2,54,87,5,3,4,6,7,87,6,4,3];
console.log(a);
//[ 1, 54, 2, 54, 87, 5, 3, 4, 6, 7, 87, 6, 4, 3 ]
function t(item){
if(item%2 == 1){
return 1;
}else{
return 0;
}
}
console.log(a.filter(t))
//[ 1, 87, 5, 3, 7, 87, 3 ]
console.log(a);
//[ 1, 54, 2, 54, 87, 5, 3, 4, 6, 7, 87, 6, 4, 3 ]
過濾奇數
var a = [1,54,2,54,87,5,3,4,6,7,87,6,4,3];
console.log(a);
//[ 1, 54, 2, 54, 87, 5, 3, 4, 6, 7, 87, 6, 4, 3 ]
function t(item){
if(item%2 == 0){
return 1;
}else{
return 0;
}
}
console.log(a.filter(t))
//[ 54, 2, 54, 4, 6, 6, 4 ]
console.log(a);
//[ 1, 54, 2, 54, 87, 5, 3, 4, 6, 7, 87, 6, 4, 3 ]
過濾偶數
var a = [1,54,2,54,87,5,3,4,6,7,87,6,4,3];
console.log(a);
//[ 1, 54, 2, 54, 87, 5, 3, 4, 6, 7, 87, 6, 4, 3 ]
function t(item){
if(item%7 == 0 || item%5 == 0){
return 1;
}else{
return 0;
}
}
console.log(a.filter(t))
//[ 5, 7 ]
console.log(a);
//[ 1, 54, 2, 54, 87, 5, 3, 4, 6, 7, 87, 6, 4, 3 ]
過濾七或五的倍數
尋找
find()
尋找第一個大於10的數
var a = [5,2,7,4,32,5,8,56,4,3,4];
console.log(a);
//[ 5, 2, 7, 4, 32, 5, 8, 56, 4, 3, 4 ]
function f(element){
return element > 10;
}
console.log(a.find(f))
//32
尋找第一個大於5且小於9的數
var a = [5,2,7,4,32,5,8,56,4,3,4];
console.log(a);
//[ 5, 2, 7, 4, 32, 5, 8, 56, 4, 3, 4 ]
function f(element){
return element > 5 && element < 9;
}
console.log(a.find(f))
//7
個別處理
forEach()
每一項加一個任意數字
var a = [0,0,0,0,0,0,0,0,0,0];
console.log(a);
//[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
function f(item, index, arr){
arr[index] = item + Math.random()%10;
}
a.forEach(f);
console.log(a)
Javascript--array
By Simba Fs
Javascript--array
- 138