js-08-Array Learning

I. Array Grammar Format

var name=[item1,item2,......]

2. Array declaration creation

var arr=new Aarray( )    //Declare an empty array object
var arr=new Array(length)   //Declare an array of a specified length
var arr=[element]                   //Declare arrays (most common method)

Note: Array declarations in js do not need to be of specified length. Array lengths in js are not fixed and will change with the number of elements.

a: Determine if it is an array type (typeOf arr value is object)

Console.log (Array.isArray (xxxx) return value true or false

3. Traverse arrays through a for loop

for(var i=0;i<arr.length;i++)
    alert(arr[i]);
}

for-in:
    for (var p in fruits){                           //Traversing Arrays and Objects
    document.write(p+'<br>')                //p Traverse subscripts,
    document.write(fruits[p]+"<br>");
}

4. The forEach() method calls each element of the array and passes the element to the callback function.Callback functions are not performed on empty arrays.

A //callback function is a function that is passed as a parameter to another function and must wait for another function to execute before it can be called

var text = "";
    fruits.forEach(myFunction);
    document.write(text) ;
    function myFunction(value) {
     text +=  value + "<br>";
    } 
    console.log(typeof fruits.forEach);    

 

 

 

5. isArray Recognition Array

var fruits = ["Banana", "Orange", "Apple", "Mango"];
    alert(Array.isArray(fruits));
        
    //Syntax: Array.isArray(obj)

 

6. toString (converting logical values to strings and returning results)

var fruits = ["Banana", "Orange", "Apple", "Mango"];
        document.write(fruits.toString("")+'<br>');
            console.log(typeof(fruits)); //Type object
            console.log(typeof(fruits.toString()));//To String to String
    join() Method can also combine all array elements into a single string.
         document.write(fruits.join("*")) ;    

 

7. Popping and Pushing, shift, unshift, delete, splice, concat, slice

The a:pop() method deletes the last element from the array.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop()
    document.write(fruits+'<br>');
    //Returns the deleted value: Banana,
    document.write(fruits.pop()+'<br>');

 

b:push() method (at the end of the array) adds a new element to the array

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi")
        document.write(fruits) ;
        //Returns the length of the new array returned by the push() method:   
        document.write(fruits.push("Kiwi")+'<br>') ;    //5

 

c:shift() deletes the first array element and "shifts" all other elements to a lower index.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift();
		document.write(fruits+'<br>') ; 
		console.log(fruits[0]);    //Orange
		document.write(fruits.shift()+'<br>') ; 

The d:unshift() method (at the beginning) adds new elements to the array and "reverse-shifts" the old elements.

var fruits = ["Banana", "Orange", "Apple", "Mango"];

  fruits.unshift("lemon")
  document.write(fruits+'<br>');
  console.log(fruits[0]); //lemon
  document.write(fruits.unshift("lemon")) ;//5 unshift() method returns the length of the new array

e:delete operator to delete, deleting an element leaves an undefined hole in the array.

var fruits = ["Banana", "Orange", "Apple", "Mango"]; 

  delete fruits[0];
  document.write(fruits+'<br>')
  console.log("The first fruit is:" + fruits[0]);

The f:splice() method can be added, deleted, and replaced.

var fruits = ["Banana", "Orange", "Apple", "Mango"];

  //The first parameter defines where new elements should be added (stitching).
       //Second parameter: Defines how many elements should be deleted.
  //Third parameter: insert element
  //The splice() method returns an array containing deleted items
  //Array elements can be replaced by splice()

  var removed = fruits.splice(1,2, "Lemon", "Kiwi");
      document.write("New Array:<br>" + fruits+"<br>");
   document.write ( "Deleted items:<br> " + removed +"<br>");
  //Array elements can be deleted by splice()
  var removed = fruits.splice(1,2);
    document.write("New Array:<br>" + fruits+"<br>");
  //Array elements can be added through splice()
  var removed = fruits.splice(0,0,"Lemon", "Kiwi");
    document.write("New Array:<br>" + fruits+"<br>");

g:concat merges (links) arrays.

  var first= [1, 2];
   var second= [3, 4, 5];
    document.write("First Array:"+first+"<br>");
    document.write("Second Array:"+second+"<br>");
  var sum= first.concat(second);
    document.write( "Merge results:" + sum+"<br>");
  var three= [6, 7, 8];
    document.write("Third Array:"+three+"<br>");
  var arr4 = first.concat( second,three);
    document.write("Merge multiple arrays:" +arr4+"<br>") ;

The h:slice() method cuts out a new array with a segment of the array.The /slice() method creates a new array.It does not delete any elements from the source array.

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];

    document.write(fruits + '<br>');
  var citrus = fruits.slice(1);//A parameter, cut from "orange" to the end
  var citrus = fruits.slice(3);//Cut out from "Apple" to the end
    document.write( citrus + "<br>" );
  console.log(citrus[0]);//Return to Orange
   //slice() is cut out from the start position to the end position, excluding the end position.
  var citrus = fruits.slice(1,3);
    document.write("Two parameters are acceptable" + citrus);//Orange,Lemon

Keywords: Javascript

Added by phpnew on Thu, 22 Aug 2019 04:44:20 +0300