Rumah >hujung hadapan web >tutorial js >Kaedah Array yang manakah untuk digunakan dalam JS?
js const fruits = ['apple', 'banana']; fruits.push('orange'); console.log(fruits); // Output: ['apple', 'banana', 'orange']
js const fruits = ['apple', 'banana', 'orange']; const lastFruit = fruits.pop(); console.log(fruits); // Output: ['apple', 'banana'] console.log(lastFruit); // Output: 'orange'
js const fruits = ['apple', 'banana', 'orange']; const firstFruit = fruits.shift(); console.log(fruits); // Output: ['banana', 'orange'] console.log(firstFruit); // Output: 'apple'
js const fruits = ['banana', 'orange']; fruits.unshift('apple'); console.log(fruits); // Output: ['apple', 'banana', 'orange']
js const fruits = ['apple', 'banana', 'orange']; fruits.splice(1, 1, 'kiwi', 'mango'); // Removes 1 element at index 1 and adds 'kiwi' and 'mango' console.log(fruits); // Output: ['apple', 'kiwi', 'mango', 'orange']
js const numbers = [1, 2, 3, 4, 5]; numbers.fill(0, 1, 4); // Fills from index 1 to 4 with 0 console.log(numbers); // Output: [1, 0, 0, 0, 5]
js const numbers = [5, 3, 8, 1]; numbers.sort(); // Sorts numbers as strings by default console.log(numbers); // Output: [1, 3, 5, 8]
js const numbers = [1, 2, 3, 4]; numbers.reverse(); console.log(numbers); // Output: [4, 3, 2, 1]
js const fruits = ['apple', 'banana', 'orange']; fruits.splice(1, 1); // Removes 1 element at index 1 console.log(fruits); // Output: ['apple', 'orange']
js const numbers = [1, 2, 3, 4, 5]; numbers.copyWithin(0, 3); // Copies elements from index 3 to 0 console.log(numbers); // Output: [4, 5, 3, 4, 5]
Atas ialah kandungan terperinci Kaedah Array yang manakah untuk digunakan dalam JS?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!