toSpliced():as a safe way to splice an array without altering the original array.
slice(start, end): 类似于 字符串.substring.
delete: 删除某个位置元素。
cars.unshift("Lemon");cars.push("Lemon");cars[cars.length] = "Lemon";const fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.splice(2, 0, "Lemon", "Kiwi"); // ["Banana", "Orange", "Lemon", "Kiwi", "Apple", "Mango"]// The first parameter (2) defines the position where new elements should be added (spliced in).// The second parameter (0) defines how many elements should be removed.// The rest of the parameters ("Lemon" , "Kiwi") define the new elements to be added.const fruits = ["Banana", "Orange", "Apple", "Mango"];let removed = fruits.splice(2, 2, "Lemon", "Kiwi");// fruits ["Banana", "Orange", "Lemon", "Kiwi"]// removed ["Apple", "Mango"]const fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.splice(0, 1); // remove 1 element from 0 indexconst months = ["Jan", "Feb", "Mar", "Apr"];const spliced = months.toSpliced(0, 1); // spliced ["Feb", "Mar", "Apr"]const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];const citrus = fruits.slice(1); // ["Orange", "Lemon", "Apple", "Mango"]// Using delete() leaves undefined holes in the array.const fruits = ["Banana", "Orange", "Apple", "Mango"];delete fruits[0];
Math.min.apply(null, arr): to find the lowest number in an array:
Math.max.apply(null, arr): to find the largest number in an array:
var numbers = [4, 2, 5, 1, 3];numbers.sort(function (a, b) { return a - b;});
转换方法
join(): 将数组的所有元素连接成一个字符串并返回这个字符串。
concat(): 用于合并两个或多个数组,并返回一个新数组。
slice(): 返回数组的一个片段或子数组。
toString(): 返回一个由数组中的每个元素转换为字符串组成并由逗号分隔的字符串。
// concat() can take any number of array arguments,// can also take strings as arguments:const myGirls = ["Cecilie", "Lone"];const myBoys = ["Emil", "Tobias", "Linus"];const myChildren = myGirls.concat(myBoys);const myChildren = arr1.concat(arr2, arr3);const myChildren = arr1.concat("Peter");
copyWithin(): copies array elements to another position in an array.
with():update elements & without inplace.
...: spread array
// The `copyWithin()` method copies array elements to another position in an array// will change existing array.const fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.copyWithin(2, 0); // Banana,Orange,Banana,Orangeconst fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];fruits.copyWithin(2, 0, 2); // Banana,Orange,Banana,Orange,Kiwi,Papaya// with() method as a safe way to update elements in an array without altering the original array.const months = ["Januar", "Februar", "Mar", "April"];const myMonths = months.with(2, "March");// ... spread arrayconst q1 = ["Jan", "Feb", "Mar"];const q2 = ["Apr", "May", "Jun"];const q3 = ["Jul", "Aug", "Sep"];const q4 = ["Oct", "Nov", "May"];const year = [...q1, ...q2, ...q3, ...q4];