Insertion and deletion of JS arrays, conversion of string arrays and numeric arrays

JS convert string to array

Comma-separated: str.split(',')

const str = '1,2,3,4,5';
console.log(str.split(',')); // ["1", "2", "3", "4", "5"]

JS convert array to string

Method: arr.toString()

const arr = ["1", "2", "3", "4", "5"];
console.log(arr.toString()); // 1,2,3,4,5

JS converts an array of numbers to an array of strings

Method: arr. map(String)

const arr = [1, 2, 3, 4, 5, 6];
console.log(arr.map(String)); // ["1", "2", "3", "4", "5", "6"]

JS converts an array of strings to an array of numbers

Method: arr. map(Number)

const arr = ['1', '2', '3', '4', '5', '6'];
console.log(arr.map(Number)); // [1, 2, 3, 4, 5, 6]

JS turns numbers into arrays

Method: num.toString().split('')

const num = 123456;
console.log(num.toString().split('')); // ["1", "2", "3", "4", "5", "6"]

JS delete the first element in the array

Method: arr.shift()

Return value: the deleted element (that is, the original first element of the array).
If the array is empty, the shift() method will do nothing and return the value undefined.
This method does not create a new array, but directly modifies the existing array. So this method will change the length of the array.

const arr = [1, 2, 3, 4, 5, 6];
arr.shift();
console.log(arr); // [2, 3, 4, 5, 6]

JS delete the last element in the array

Method: arr. pop()

Return value: the deleted element (that is, the original last element of the array).
If the array is empty, the pop() method will do nothing and return undefined.
This method does not create a new array, but directly modifies the existing array. So this method will change the length of the array.

const arr = [1, 2, 3, 4, 5, 6];
arr.pop();
console.log(arr); // [1, 2, 3, 4, 5]

JS inserts elements after the array

Method 1: arr.push(value1, value2)

const arr = [1, 2, 3, 4];
arr.push(6, 7);
console.log(arr); // [1, 2, 3, 4, 6, 7]

Method 2: Use the spread operator (…)

const arr = [1, 2, 3, 4, 5];
const newArr = [...arr, 6, 7];
console.log(newArr); // [1, 2, 3, 4, 5, 6, 7]

JS inserts elements in front of the array

Method 1: arr.unshift(value1, value2)

const arr = [1, 2, 3, 4];
arr.unshift(6, 7);
console.log(arr); // [6, 7, 1, 2, 3, 4]

Method 2: Use the spread operator (…)

const arr = [1, 2, 3, 4, 5];
const newArr = [6, 7, ...arr];
console.log(newArr); // [6, 7, 1, 2, 3, 4, 5]

Method 3: newArr.concat(arr)

const arr = [1, 2, 3, 4, 5];
const newArr = [6, 7];
console.log(newArr.concat(arr)); // [6, 7, 1, 2, 3, 4, 5]

JS inserts elements at the specified position in the array

Insert an element in the second position of the array: arr.splice(2, 0, value1, value2)

array.splice(index, 0, insertValue), the return value is an empty array, and the array value is the final result value

index: the first parameter (insert position)

0: The second parameter (fixed to 0[Number to be deleted])

insertValue: the third parameter (inserted items [can be multiple, separated by commas])

const arr = [1, 2, 3, 4];
arr.splice(2, 0, 6, 7);
console.log(arr); // [1, 2, 6, 7, 3, 4]

The difference between push, push.apply and concat

push adds a new array to the end of the original array, which changes the original array

const tempArr = [1, 2, 3, 4];
tempArr.push(5);
console.log(tempArr); // [1, 2, 3, 4, 5]

Add the appended array as an item of an element to the end of the original array

const tempArr = [1, 2, 3, 4];
tempArr.push([6, 7]);
console.log(tempArr); // [1, 2, 3, 4, [6, 7]]

push.apply pushes the value of the latter array into the former array in turn, so that the former array changes, and only two arrays can be merged, which will change the original array

const arr1 = [1, 2, 3, 4];
const arr2 = [4, 5, 6, 7];
arr1.push.apply(arr1, arr2);
console.log(arr1); // [1, 2, 3, 4, 4, 5, 6, 7]

After concat merges arrays, the return value is a new array, and two or more arrays can be merged without changing the original array

const tempArr = [1, 2, 3, 4];
const newArr = tempArr.concat([7, 8]);
console.log(newArr); // [1, 2, 3, 4, 7, 8]

Summary: push and push.apply will change the original array; concat will not change the original array, and the return value is the new array

Guess you like

Origin blog.csdn.net/AdminGuan/article/details/127991294