Several common methods of arrays

Source: Internet
Author: User

Several common methods of arrays

Unshift () added, adding the given value to the beginning of the array

var arr1=["The King Cabbage", "Niu Chun", "wild old many"];
Console.log (Arr1.unshift ("Big face", "Old Ann");//5
Console.log (ARR1);
["Big Face", "Lao Ann", "King cabbage", "Niu Chun", "Wild old")

var arr = [1, 2];

Arr.unshift (0);
Arr.unshift (-2,-1); -2,-1, 0, 1, 2
Arr.unshift ([-3]);
Console.log (arr);

The shift () method removes the first element from the array and returns the value of the element. This method changes the length of the array.
The element that is removed from the array; Returns undefined if the array is empty.
Let A = [1, 2, 3];
Let B = a.shift ();
Console.log (a);
[2, 3]
Console.log (b);
1

Push () Adds the value to be given to the back of the array
var arr2=["The King Cabbage", "Niu Chun", "wild old many"];
Console.log (Arr2.push ("Big face", "Old Ann");//5
Console.log (ARR2);//"King cabbage", "Ox Spring", "Wild old", "Big Face", "Old Ann"

The Pop () method removes the last element from the array and returns the value of the element. This method changes the length of the array.
Let A = [1, 2, 3];
A.length; 3
A.pop (); 3
Console.log (a); [1, 2]
A.length; 2

Splice (beginning [starting from 1], (length [not deleted is 0, delete a few are several], element A, element B)
var arr=["The King Cabbage", "Niu Chun", "wild old many"];
Arr.splice (1, 0, ' Old Ann ');
Console.log (arr);//"King cabbage", "Old Ann", "Niu Chun", "Wild old"
You want to start by adding a few
For example, after the first bit, add "old Ann", "tablet",
Arr.splice (1, 0, "Old Ann", "tablet");
Console.log (arr);//"King cabbage", "Old Ann", "tablet", "Ox Spring", "Wild Old More"
Remove the ox spring from the first place, add "old Ann", "tablet",
Arr.splice (1, 1, "Old Ann", "tablet");
Console.log (arr);//"King cabbage", "Old Ann", "tablet", "wild old
Arr.splice (1, 0);
Console.log (arr); "King cabbage", "Niu Chun", "Wild Old More"

Concat () Merge, combining two numbers and
var arr1 = [' A ', ' B ', ' C '];
var arr2 = [' d ', ' e ', ' F '];
======= Merging =====
Console.log (Arr1.concat (ARR2))//["A", "B", "C", "D", "E", "F"]

======== Method One ========
Console.log (Arr1.concat ("J", ARR2))//["A", "B", "C", "J", "D", "E", "F"]

Console.log (Arr1.concat ("J" +arr2));//["A", "B", "C", "Jd,e,f"]

=========== Method 2===========
Console.log (Arr1+arr2.concat ("J"));//stitching together A,b,cd,e,f,j

Join () Converts all elements of an array (or a class array object) into a string
var arr2=["The King Cabbage", "Niu Chun", "wild old many"];
Console.log (Arr2.join ());//King cabbage, ox spring, wild old
Console.log (Arr2.join ("));//breaking Beef cattle Spring Wild Lao Many
Console.log (Arr2.join ('-'));//King Cabbage-Niu Chun-Wild old

The reverse () method is to reverse the order of the array
var arr = [' One ', ' both ', ' three '];
var reversed = Arr.reverse ();
Console.log (arr);//["Three", "one", "one"
Console.log (reversed);

Sort () sorting
var arr = [1, 30, 22, 49, 31, 21];
Arr.sort ();
Console.log (arr);//[1, 21, 22, 30, 31, 49]
Arr.sort (function (A, b) {
return a-b;//Ascending [1, 21, 22, 30, 31, 49]
return b-a;//Descending [49, 31, 30, 22, 21, 1]
//});
Console.log (arr);

Filtering of the filter () array
Example: Filtering words with a word length greater than 6 in an array
let words = [' SPR ', ' limit ', ' like ', ' destruction ', ' present '];
Let result = words.filter (Word = word.length > 6);
Console.log (Result)

Several common methods of arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.