es6 Array數組方法

es6 Array數組方法 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>arr方法</title>
<script>


// Array.from() 方法從一個相似數組或可迭代對象中建立一個新的數組
// const bar=["a","b","c"];
// alert(Array.from(bar));
// alert(Array.from('foo'));


// Array.isArray() 用於肯定傳遞的值是不是一個 Array。
// alert(Array.isArray([1,2,3]));//true
// alert(Array.isArray({foo:123}));//false
// alert(Array.isArray("foobar"));//false
//Array.of() 方法建立一個具備可變數量參數的新數組實例,而不考慮參數的數量或類型。

// Array.of() 和 Array 構造函數之間的區別在於處理整數參數:Array.of(7) 建立一個具備單個元素 7 的數組,而 Array(7) 建立一個包含 7 個 undefined 元素的數組

// alert(Array.of(7));[7]
// alert(Array.of(1,2,3));//[1,2,3];
// alert(Array(7));//[7]
// alert(Array(1,2,3));//[1,2,3];

// every() 方法測試數組的全部元素是否都經過了指定函數的測試

// 下例檢測數組中的全部元素是否都大於 10
// function isBigEnough(element,index,array) {
// return (element >= 10);
// }
// var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// alert(passed);//false
// passed=[12, 54, 18, 130, 44].every(isBigEnough);// true
// alert(passed)
// fill() 方法用一個固定值填充一個數組中從起始索引到終止索引內的所有元素。
// var array1 = [1, 2, 3, 4];
// // fill with 0 from position 2 until position 4
// console.log(array1.fill(0, 2, 4));
// // expected output: [1, 2, 0, 0]

// // fill with 5 from position 1
// console.log(array1.fill(5, 1));
// // expected output: [1, 5, 5, 5]

// // console.log(array1.fill(6));
// // expected output: [6, 6, 6, 6]

// filter() 方法建立一個新數組, 其包含經過所提供函數實現的測試的全部元素。 
// function isBigEnough(value) {
// return value >= 10;
// }
// var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// alert("數組"+filtered);
// const isBigEnough = value => value >= 10;
// let [...spread]= [12, 5, 8, 130, 44];

// let filtered = spread.filter(isBigEnough);
// alert(filtered);

// find() 方法返回數組中知足提供的測試函數的第一個元素的值。不然返回 undefined。
// function isBigEnough(element) {
// return element >= 15;
// } 
// alert([12, 5, 8,10, 44].find(isBigEnough));
// findIndex()方法返回數組中知足提供的測試函數的第一個元素的索引。不然返回-1
// function isBigEnough(element) {
// return element >= 15;
// }
// alert([12, 5, 8, 130, 44].findIndex(isBigEnough)); //3

// indexOf()方法返回在數組中能夠找到一個給定元素的第一個索引,若是不存在,則返回-1
// let a=[2, 9, 7, 8, 9]; 
// // alert(a.indexOf(2)); // 0 
// // alert(a.indexOf(6)); //-1
// // a.indexOf(7); // 2
// // a.indexOf(8); // 3
// alert(a.indexOf(9)); // 1

// if (a.indexOf(3) === -1) {
// // element doesn't exist in array
// }

// join() 方法將一個數組(或一個類數組對象)的全部元素鏈接成一個字符串並返回這個字符串。
// let a=['Wind','Rain','Fire'];
// console.log(a.join("*")); // 'Wind*Rain*Fire'


// map() 方法建立一個新數組,其結果是該數組中的每一個元素都調用一個提供的函數後返回的結果。
// let numbers = [1, 5, 10, 15];
// let doubles = numbers.map( x => x ** 2);
// alert(numbers); //[1, 5, 10, 15]
// alert(doubles);//[1, 25, 100, 225]

// pop()方法從數組中刪除最後一個元素,並返回該元素的值。此方法更改數組的長度。
// let a = [1, 2, 3];
// console.log(a.length); // 3
// a.pop(); // 3

// console.log(a); // [1, 2]
// // a.length; // 2


// push() 方法將一個或多個元素添加到數組的末尾,並返回新數組的長度。
// var numbers=[1,2,3];
// numbers.push(4);
// console.log(numbers); // [1, 2, 3, 4]

// numbers.push(5, 6, 7);

// console.log(numbers); // [1, 2, 3, 4, 5, 6, 7]

// shift() 方法從數組中刪除第一個元素,並返回該元素的值。此方法更改數組的長度。
// let a = [1, 2, 3];
// let b = a.shift();
// console.log(a); // [2, 3]
// console.log(b); // 1


// slice() 方法返回一個從開始到結束(不包括結束)選擇的數組的一部分淺拷貝到一個新數組對象。原始數組不會被修改
// var animals=['ant','bison','camel','duck','elephant'];
// console.log(animals.slice(4));//刪除第4個

// sort() 方法用就地( in-place )的算法對數組的元素進行排序,並返回數組。 sort 排序不必定是穩定的。默認排序順序是根據字符串Unicode碼點。
// var fruit = ['cherries', 'apples', 'bananas'];
// alert(fruit.sort());

// var scores = [1, 10, 21, 2]; 
// console.log(scores.sort());
// 
// splice() 方法經過刪除現有元素和/或添加新元素來更改一個數組的內容。
// var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];//索引從1開始

// myFish.splice(2, 0,'drum'); // 在索引爲2的位置插入'drum'
// console.log(myFish)// myFish 變爲 ["angel", "clown", "drum", "mandarin", "sturgeon"]

// // myFish.splice(2, 1); // 從索引爲2的位置刪除一項(也就是'drum'這一項)
// // // myFish 變爲 ["angel", "clown", "mandarin", "sturgeon"]

// unshift() 方法將一個或多個元素添加到數組的開頭,並返回新數組的長度。
// let a=[1,2,3];
// a.unshift(4,5);
// console.log(a);// [4, 5, 1, 2, 3]

 

</script>
</head>
<body>

</body>
</html>
相關文章
相關標籤/搜索