Basic methods of JS array operation

Source: Internet
Author: User

1. Array detection

var newArr = [];newarr  instanceof Array;   // true Array.isarray (NEWARR);   // true // Array.isarray (NEWARR) only supports Ie9+,firefox 4+,safari 5+,opera 10.5+ and Chrome

2. Creating an array

var New // Create an array var New Array (3);  // Create an array and specify the length var New Array (1,2,[3,4]);  //

3. Adding elements

Newarr.push (' A ');  // 6newArr;  // [1,2,3,4,5, ' A '] Newarr.unshift (//7//  [' B ', 1,2,3,4,5, ' A ']newarr.splice (  //  []//["B", 1, "C", 2, 3, 4, 5, "A"]

4. Deleting elements

var newArr = [1,2,3,4,5];newarr.pop ();   // 5newArr;  // [1,2,3,4]  //1newArr;  // [2,3,4] Newarr.splice (//[3,4]newArr;  // [2]

5. Intercepting and merging elements

var newArr = [' A ', ' B ', ' C ', ' d ', ' E '];newarr.slice (//["C", "D", "E"]// ["B", "C", "D"] newarr.concat (' M ');  // ["A", "B", "C", "D", "E", "M"]

6, copy of the array

var newArr = [' A ', ' B ', ' C ', ' D ', ' E '];newarr.slice (//["A", "B", "C", "D", "E"] Newarr.concat ();  // ["A", "B", "C", "D", "E"]

7. Sorting of arrays

var newArr = [' A ', ' C ', ' B ', ' D ', ' M '];newarr.reverse ();   // ["M", "D", "B", "C", "A"]newarr.sort ();  // "A", "B", "C", "D", "M"]

8. String conversion of arrays

var  NEWARR = [];newarr.join (', ');  // "Newarr.tostring"();  // " the" // "newarr.valueof"();  // [A] // tolocalestring, toString, valueOf: Can be seen as a special use of joins, not commonly used

9. Array Lookup

varNEWARR = [1,2,3,4,5,4,3,2,3,4];newarr.indexof (3);//first occurrence of element 3 location: 2Newarr.indexof (3,4);//starting from position 3 Find the first occurrence of element 4 in the location: 6Newarr.lastindexof (3);//last occurrence of element 3 location: 8Newarr.lastindexof (2,4);//starting at position 4 finding the last occurrence of element 2:1varPerson={name: "Lili"}, Person1=[{name: "Lili"}], Person2=[Person];p erson1.indexof (person);//return-1 must be strictly equal, not just value equalPerson2.indexof (person);//returns 0//indexOf () from the go after looking, not found for-1//lastIndexOf () Looking forward from the back, not found for-1

Note: Self-search data collation, for reference only, thank you ~ ~

Basic methods of JS array operation

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.