Objek Array
dalam JavaScript, seperti tatasusunan dalam bahasa pengaturcaraan lain, ialah koleksi data. Dalam JavaScript, data di dalam tatasusunan boleh terdiri daripada jenis yang berbeza dan mempunyai kaedah untuk melaksanakan operasi biasa pada tatasusunan.
const hobbys = new Array() hobbys[0] = 'Basketball' hobbys[1] = 'Badminton' hobbys[2] = 'swimming' console.log(hobbys) // [ 'Basketball', 'Badminton', 'swimming' ]
const hobbys = new Array('Basketball', 'Badminton','swimming') console.log(hobbys) // [ 'Basketball', 'Badminton', 'swimming' ]
const hobbys = ['Basketball','Badminton','swimming'] console.log(hobbys) // [ 'Basketball', 'Badminton', 'swimming' ]
2 peta
// currentValue:必需,当前元素 index:可选,当前元素的索引值 arr:可选,当前元素所属的数组对象。 array.forEach(function(currentValue, index, arr))
let array = ['a', 'b', 'c'] let func = (currentValue, index, arr) => { currentValue += 's' console.log('currentValue:' + currentValue + ' index:' + index + ' arr:' + arr) } array.forEach(func) console.log(array) // 控制台输出: // currentValue:as index:0 arr:a,b,c // currentValue:bs index:1 arr:a,b,c // currentValue:cs index:2 arr:a,b,c // [ 'a', 'b', 'c' ]
Kaedah map() mengembalikan tatasusunan baharu, dan elemen dalam tatasusunan ialah nilai elemen tatasusunan asal selepas memanggil fungsi tersebut. Kaedah memproses elemen secara berurutan dalam susunan elemen tatasusunan asal. Tatasusunan asal tidak akan diubah.
3. concat// currentValue:必须,当前元素的值 index:可选,当前元素的索引值 arr:可选,当前元素属于的数组对象 array.map(function(currentValue,index,arr))
let array = [1, 2, 3, 4, 5] let result = array.map((item) => { return item += 5 }) console.log(array) console.log(result) // [ 1, 2, 3, 4, 5 ] // [ 6, 7, 8, 9, 10 ]
concat()
// array1, array2, ..., arrayN 必需,该参数可以是具体的值,也可以是数组对象,可以是任意多个 array1.concat(array2,array3,...,arrayN)
const array1 = ['a', 'b', 'c'] const array2 = ['d', 'e', 'f'] const array3 = array1.concat(array2) console.log(array3) const array4 = array1.concat('123') console.log(array4) // [ 'a', 'b', 'c', 'd', 'e', 'f' ] // [ 'a', 'b', 'c', '123' ]
push()
5 unshift
let fruits = ["Banana", "Orange", "Apple", "Mango"] let length = fruits.push("Kiwi") console.log(fruits) console.log(length) // [ 'Banana', 'Orange', 'Apple', 'Mango', 'Kiwi' ] // 5
6 pop
let fruits = ["Banana", "Orange", "Apple", "Mango"] let length = fruits.unshift("Lemon", "Pineapple") console.log(fruits) console.log(length) // [ 'Lemon', 'Pineapple', 'Banana', 'Orange', 'Apple', 'Mango' ] // 6
7 anjakan
let sites = ['Google', 'Runoob', 'Taobao', 'Zhihu', 'Baidu'] let result = sites.pop() console.log(sites) console.log(result) // [ 'Google', 'Runoob', 'Taobao', 'Zhihu' ] // Baidu
8 kaedah splice
let fruits = ["Banana", "Orange", "Apple", "Mango"]; let result = fruits.shift() console.log(fruits) console.log(result) // [ 'Orange', 'Apple', 'Mango' ] // Banana
9 slice
// 参数 Values: index: 必需,规定从何处添加/删除元素 // howmany: 可选,规定应该删除多少元素 必须是数字,但可以是 "0" // item1, ..., itemX 可选,要添加到数组的新元素 array.splice(index,howmany,item1,.....,itemX)
let fruits = ["Banana", "Orange", "Apple", "Mango"] let result = fruits.splice(1, 2, "Lemon", "Kiwi") console.log(fruits) console.log(result) // [ 'Banana', 'Lemon', 'Kiwi', 'Mango' ] // [ 'Orange', 'Apple' ]
10 kaedah join
// start: 可选,规定从何处开始选取 若为负值,表示从原数组中的倒数第几个元素开始提取 // end: 可选,规定从何处结束选取 如果没有指定该参数,那么切分的数组包含从start到数组结束的所有元素 array.slice(start, end)
let fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"] let result1 = fruits.slice(1, 3) let result2 = fruits.slice(2) console.log(fruits) console.log(result1) console.log(result2) // [ 'Banana', 'Orange', 'Lemon', 'Apple', 'Mango' ] // [ 'Orange', 'Lemon' ] // [ 'Lemon', 'Apple', 'Mango' ]
11 setiap
// separator: 可选,指定要使用的分隔符 如果省略该参数,则使用逗号作为分隔符 array.join(separator)
let fruits = ["Banana", "Orange", "Apple", "Mango"]; let energy1 = fruits.join(); let energy2 = fruits.join('-'); console.log(energy1) console.log(energy2) // Banana,Orange,Apple,Mango // Banana-Orange-Apple-Mango
12 penapis
array.every(function(currentValue,index,arr))
let ages = [32, 33, 16, 40] let nums = [32, 33, 19, 40] function checkAdult(age) { return age >= 18 } function checkNums(num) { return num >= 18 } // 16不满足大于18,故结果false let result1 = ages.every(checkAdult) // 每一项都满足条件,故结果true let result2 = nums.every(checkNums) console.log(result1) console.log(result2) // false // true
13 kaedah indexOf
array.filter(function(currentValue,index,arr), thisValue)
let ages = [32, 33, 16, 40]; function checkAdult(age) { return age >= 18; } let result = ages.filter(checkAdult) console.log(result) // [ 32, 33, 40 ]
14 kaedah reduce
// searchvalue: 必需。规定需检索的字符串值。 // start: 可选的整数参数。规定在字符串中开始检索的位置。值:0~array.length-1 string.indexOf(searchvalue,start)
let str = "Hello world, welcome to the universe."; // 输出w所在的下标索引13(空格也算),没有找到会返回-1 let n = str.indexOf("welcome"); console.log(n) console.log(str[n]) // 13 // w
15 terbalik
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
let numbers = [2, 3, 5, 6] function getSum(total, num) { return total + num } let result = numbers.reduce(getSum, 0) console.log(result) // 16
16. Isih
let fruits = ["Banana", "Orange", "Apple", "Mango"] let resut = fruits.reverse() console.log(fruits) console.log(resut) // [ 'Mango', 'Apple', 'Orange', 'Banana' ] // [ 'Mango', 'Apple', 'Orange', 'Banana' ]
17 toString
// sortfunction: 可选。规定排序顺序。必须是函数。 array.sort(sortfunction)
let fruits = ["Banana", "Orange", "Apple", "Mango"] let ages = [9, 3, 4, 5, 7, 10] // 升序 let agesFunAsc = function (ag1,ag2) { return ag1 - ag2 } // 降序 let agesFunDes= function (ag1,ag2) { return -(ag1 - ag2) } fruits.sort() ages.sort(agesFunAsc) console.log(fruits) console.log(ages) ages.sort(agesFunDes) console.log(ages) // [ 'Apple', 'Banana', 'Mango', 'Orange' ] // [ 3, 4, 5, 7, 9, 10 ] // [ 10, 9, 7, 5, 4, 3 ]
18 Kaedah at
number.toString(radix)
let num = 15 let n = num.toString() // 也可以使用不同的进制把一个数字转换为字符串 // 2进制 let b = num.toString(2); // 8进制 let c = num.toString(8); // 16进制 let d = num.toString(16); console.log(n) console.log(b) console.log(c) console.log(d) // 15 // 1111 // 17 // f
at()
19. kaedah find
array.at(index)
let str = 'helso word' let item1 = str.at(2) let item2 = str.at(-1) console.log(item1) console.log(item2) // l // d
20 beberapa
array.find(function(currentValue, index, arr),thisValue)
let ages = [3, 10, 18, 20]; function checkAdult(age) { return age >= 18; } let value = ages.find(checkAdult) console.log(value) // 18
Atas ialah kandungan terperinci Ringkasan 20 operasi tatasusunan JavaScript biasa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!