Let’s tidy up the method of JavaScript array.
Add/Delete Array
Array.push()
Add the last element of the array.
let number_arr = [1, 2, 3, 4, 5 ];
number_arr.push(6);
console.log(number_arr); // [1, 2, 3, 4, 5, 6]
Output :
[1, 2, 3, 4, 5, 6]
Array.unshift()
Add the first element of the array.
let number_arr = [1, 2, 3, 4, 5 ];
number_arr.unshift(6);
console.log(number_arr); // [6, 1, 2, 3, 4, 5]
Output :
[6, 1, 2, 3, 4, 5]
Array.pop()
Delete the last element of the array.
let number_arr = [1, 2, 3, 4, 5 ];
let pop = number_arr.pop();
console.log(number_arr); // [1, 2, 3, 4]
console.log(pop); // 5
Output :
[1, 2, 3, 4] 5
Array.shift()
Delete the first element in the array.
let number_arr = [1, 2, 3, 4, 5 ];
let shift = number_arr.shift();
console.log(number_arr); // [2, 3, 4, 5]
console.log(shift); // One
Output :
[2, 3, 4, 5] 1
Array.slice()
The existing array remains unchanged, and returns that element.
let number_arr = [1, 2, 3, 4, 5 ];
// Syntax: Array.slice (start position, end position)
let sp = number_arr.slice(1, 3); // Return from 2nd to 3rd.
console.log(number_arr); // [1 2 3 4 5]
console.log(sp); // [2, 3]
sp = number_arr.slice(2);
console.log(sp); // [3, 4, 5]
Output :
[1, 2, 3, 4, 5] [2, 3] [3, 4, 5]
Array.splice()
Delete the array at the specified position.
let number_arr = [1, 2, 3, 4, 5 ];
// Syntax: Array.splice(start position, number to delete, add_to_item...)
let sp = number_arr.splice(1, 2); // Delete 2 from the 2nd element.
console.log(number_arr); // [1, 4, 5]
console.log(sp); // [2, 3]
let sp2 = number_arr.splice(1, 0, 7, 8, 9); // Delete 0 at the second element position and add 7,8,9.
console.log(number_arr); // [1, 7, 8, 9, 4, 5]
console.log(sp2); // undefined: 0 is deleted, so there is no value.
Output :
[1, 4, 5] [2, 3] [1, 7, 8, 9, 4, 5] []
Array.concat()
Combine the arrays.
let num1 = [1, 2, 3];
let num2 = [4,5,6];
let combine = num1.concat(num2);
console.log(combine); // [1, 2, 3, 4, 5, 6]
let num3 = [7, 8, 9];
combine = num1.concat(num2, num3);
console.log(combine); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
Output :
[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 7, 8, 9]
Array.of()
Create a new instance array as a function argument.
let ar = Array.of(1, 2, 3)
console.log(ar); // [1, 2, 3]
let ch = Array.of("Chibi", "Champon");
console.log(ch); // ["Chibi", "Champon"]
Output :
[1, 2, 3] [“Jajang”, “Champon”]
Array iteration
Array.forEach()
Iterate through the elements of an array sequentially.
let vl = ['one','two','three' ];
vl.forEach((e) => {
console.log(e); // print one, two, three in sequence
})
Output :
one two three
The second argument returns the index of the element.
let vl = ['one','two','three' ];
vl.forEach((e, i) => {
console.log(e, i);
})
Output :
one 0 two 1 three 2
Array.values()
Make the elements of the array a new iterative object.
let vl = ['one','two','three' ];
let iterator = vl.values();
console.log(iterator.next().value); // one
console.log(iterator.next().value); // two
console.log(iterator.next().value); // three
console.log(iterator.next().value); // undefined
Output :
one two three undefined
let vl = ['one','two','three' ];
let iterator = vl.values();
for (let i of iterator) {
console.log(i);
}
console.log(iterator.next().value); // undefined: None of the above are repeated.
Output :
one two three
Array.entries()
Make an element of an array into a repeatable object paired by key and value.
let vl = ['one','two','three' ];
let iterator = vl.values();
for (let i of iterator) {
console.log(i);
}
Output :
[0, "one”] [1, "two”] [2, "three”]
Find Array
Array.some()
Returns true if any element in the array matches the conditional expression.
console.log([2, 5, 8, 1, 4].some((e) => e> 5));
console.log([2, 5, 8, 1, 4].some((e) => e> 9));
Output :
true false
Array.every()
All elements in the array must match the conditional expression to return true.
console.log([2, 5, 8, 1, 4].every((e) => e <9));
console.log([2, 5, 8, 1, 4].every((e) => e> 4));
Output :
true false
Array.find()
Returns the first element of a conditional expression in an array.
console.log([2, 5, 8, 1, 4].find((e) => e <9));
console.log([2, 5, 8, 1, 4].find((e) => e> 4));
Output :
2 5
The second value is the index of the array, the third is the current array.
console.log([2, 5, 8, 1, 4].find((e, i) => e> 3 && i <2));
Output :
5
Array.findIndex()
Returns the index of the first element in the array that meets the conditional expression.
console.log([2, 5, 8, 1, 4]