strings and Arrays in JavaScript

Source: Internet
Author: User

The operation string and operation array in JS are similar.

Take the following array as an example:

var arr = ['e'c'r ' ' o ' ]; var arrnum = [64];

1, determine whether the array

if (arr instanceof Array) {    Console.log (true);} if // ECMAScript 5    Console.log (true);}

2. Format as a string

Console.log (Arr.join (','));

3. Add Delete

Arr.push (' x '//////["C", "R", "O"] arr.unshift (' d '//["D", "C", "R", "O"]

4. Sorting method

Console.log (Arr.reverse ());//["O", "R", "C", "D"]Console.log (Arr.sort ());//["C", "D", "O", "R"]Console.log (Arrnum.sort ());//Convert to String after comparison [4, 6, +]Console.log (Arrnum.sort (Compare));//[4, 6, up, up]Console.log (Arrnum.sort (Compareex));//[4, 6, up, up]functionCompare (A, b) {if(A <b) {return-1; } Else if(A >b) {return1; } Else {        return0; }}functionCompareex (A, b) {returnAb;}

5. Operation method

var arrnew = Arr.concat (' Red ', [' ss ', ' Z '//
Console.log (Arr.slice (2));      // ["R", "O"]console.log (Arr.slice (1, 3));   // ["C", "R"]
//Splice The most powerful return value for the deleted composition of the array itself changesConsole.log (Arr.splice (1, 3));//["C", "R", "O"]Console.log (arr);//["E"]vararr = [' E ', ' C ', ' R ', ' O ']; Console.log (Arr.splice (2, 0, ' add ', ' DD '));//[]Console.log (arr);//["E", "C", "Add", "DD", "R", "O"]vararr = [' E ', ' C ', ' R ', ' O '];console.log (Arr.splice (2, 2, ' Ed ', ' fr '));//["R", "O"]Console.log (arr);//["E", "C", "Ed", "Fr"]

6. Position Method ECMAScript 5

var arr = [' E ', ' C ', ' R ', ' O '];console.log (arr.indexof (//-1//1 // 1

7. Iterative Method ECMAScript 5

varnum = [1, 3, 5, 10, 56, 11];varR = Num.every (function(item, index, array) {//all satisfies return true    return(item% 2 = = 0);}); Console.log (R); R= Num.filter (function(item, index, array) {//Filter    return(item% 2 = = 0);}); Console.log (R); R= Num.some (function(item, index, array) {//returns True if one matches    return(item% 2 = = 0);}); Console.log (R); R= Num.map (function(item, index, array) {//returns the new array    return(item-1);}); Console.log (r);//for each actionNum.foreach (function(item, index, array) {Console.log (item);});

8, reduce the method ECMAScript 5

//parameters: Previous item, current item, ordinal, arrayvarnum = [1, 3, 5, 10, 56, 11];varsum = Num.reduce (function(pre, cur, index, array) {returnPre +cur;});  Console.log (sum); // thevarsum = Num.reduceright (function(pre, cur, index, array) {Console.log (pre+ ' +cur); returnPre-cur;}); Console.log (sum); //-64

9. String

var name = ' Zjzhome '; Console.log (Name.charat (1));  // J Console.log (name.substring (/// and slice like preferred slice//jz//Home // Jzh

String and array in JavaScript

Related Article

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.