All methods in array (super detailed)

All methods of array
1. concat () joins elements into an array.
Example:
var str1 = "Hello ";
var str2 = "world!";
var n = str1.concat(str2);
Hello world!  Output result

  

 

The 2 ‚ every() method uses the specified function to detect all elements in the array:
  • If one element in the array is not satisfied, the entire expression returns false, and the remaining elements will not be detected.
  • Returns true if all elements meet the criteria.

Note: every() does not detect empty arrays.

Note: every() does not change the original array.

Example:

var ages = [32, 33, 16, 40];

function checkAdult(age) {
    return age >= 18;
}

function myFunction() {
    document.getElementById("demo").innerHTML = ages.every(checkAdult);
}


false  Output result

  

 

3. filter () returns the array elements that satisfy the assertion function.
 
var ages = [32, 33, 16, 40];

function checkAdult(age) {
    return age >= 18;
}

function myFunction() {
    document.getElementById("demo").innerHTML = ages.filter(checkAdult);
}
32,33,40 Output result

  

 

4. forEach() calls the specified function for each element of the array.

 

<button onclick="numbers.forEach(myFunction)">Point me</button>
<p id="demo"></p>
 
<script>
demoP = document.getElementById("demo");
var numbers = [4, 9, 16, 25];
 
function myFunction(item, index) {
    demoP.innerHTML = demoP.innerHTML + "index[" + index + "]: " + item + "<br>"; 
}
</script>

//Output result
index[0]: 4
index[1]: 9
index[2]: 16
index[3]: 25

 

5. indexOf() finds the specified element in the array.

 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");

//Result 2

 

6. join () converts all elements of the array into strings.

 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join();
//Result
Banana,Orange,Apple,Mango

 

7. lastIndexOf() searches the array in reverse.

 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.lastIndexOf("Apple");
//Result 2

 

8. map() calculates the new array from the elements of the array.

 

var numbers = [4, 9, 16, 25];

function myFunction() {
    x = document.getElementById("demo")
    x.innerHTML = numbers.map(Math.sqrt);
}
//Result  2,3,4,5

 

9. pop () removes the last element of the array.

 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();
//Return to Banana,Orange,Apple

 

10. push () adds elements to the end of the array.

 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi")
//Result output
Banana,Orange,Apple,Mango,Kiwi

  

11. reduce () calculates a value from the elements of the array.

 

var numbers = [65, 44, 12, 4];
 
function getSum(total, num) {
    return total + num;
}
function myFunction(item) {
    document.getElementById("demo").innerHTML = numbers.reduce(getSum);
}
//Output result 125

  

12. Reduce right() reduces the array from right to left.

 

var numbers = [65, 44, 12, 4];
 
function getSum(total, num) {
    return total + num;
}
function myFunction(item) {
    document.getElementById("demo").innerHTML = numbers.reduceRight(getSum);
}
//Output result 125

 

13. reverse () reverses the order of elements in the original array.

 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.reverse();
//Output result
Mango,Apple,Orange,Banana

 

14. shift () removes the first element of the array.

 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift()
//Result
Orange,Apple,Mango

 

15. slice() returns a part of the array.

 

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3);
//Result output Orange,Lemon

 

16. some () tests whether at least one array element can make the assertion function true.

 

var ages = [3, 10, 18, 20];

function checkAdult(age) {
    return age >= 18;
}

function myFunction() {
    document.getElementById("demo").innerHTML = ages.some(checkAdult);
}
//Output result true

 

17. sort () sorts the array elements in the original array.

 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
//Output results Apple,Banana,Mango,Orange

 

18. splice() inserts, deletes, or replaces array elements.

 

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi");
//Output results Banana,Orange,Lemon,Kiwi,Apple,Mango

 

19. toLocaleSTring() converts an array to a localized string.

 

var d=new Date();
var n=d.toLocaleString();
Output results 4:59:41 pm on November 7, 2019

 

20. toString() converts an array to a string.

   

var d=new Date();
var n=d.toString();
Thu Nov 07 2019 17:00:09 GMT+0800 (China standard time)

 

21. unshift() inserts elements in the array header.
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple");
fruits Output:

Lemon,Pineapple,Banana,Orange,Apple,Mango

 

By Gao Ruiting

QQ: 2419056691

Tags: Javascript

Posted on Thu, 07 Nov 2019 01:50:55 -0800 by Journey44