Array manipulation in JS

Source: Internet
Author: User

Array API
    • Api:application programming Interface, application programming interface;
    • JS in the object provided by the method is called the API;
instanceof
检测一个对象是否是数组;(用来对付复杂数据类型;)// 简单数据类型 typeof ;A instanceof B  // A是不是B造出来的;例:    var arr = [1,2,3];    console.log(arr instanceof Array); //arr属不属于Array类型;
Array.isarray ()
Array.isArray(参数); // 判断参数是不是数组,返回布尔值;例:    var arr = [1,2,3];    var num = 123;    console.log(Array.isArray(arr)); //true    console.log(Array.isArray(num)); //false
ToString ()
数组.toString(); // 把数组变成字符串,去除了[],内容用逗号链接;例:    var arr = ["aaa","bbb","ccc"];    console.log(arr.toString());      //返回 aaa,bbb,ccc
ValueOf ()
数组.valueOf();  //返回数组本身;   例:    var arr = ["aaa","bbb","ccc"];    console.log(arr.valueOf());      //返回数组本身  ["aaa","bbb","ccc"]
Array. Join (parameter)
数组.join(参数);  // 数组中的元素可以按照参数进行链接变成一个字符串;console.log(arr.join());    //和toString()一样用逗号链接console.log(arr.join("|")); //用参数链接console.log(arr.join("&")); //用参数链接console.log(arr.join(" ")); //如果是空格,真的用空格链接console.log(arr.join(""));  //空字符是无缝连接
Adding and removing of array elements push () and pop ()
1. 数组.push() //在数组的最末尾添加元素;2. 数组.pop()  //不需要参数;在数组的最末尾删除一项;例:    var arr = [1,2,3];    var aaa = arr.push("abc");//在数组的最末尾添加一个元素;    console.log(arr);//元素被修改了    console.log(aaa);//返回值是数组的长度;    aaa = arr.pop();//不需要参数;在数组的最末尾删除一项;    console.log(arr);//元素被修改了    console.log(aaa);//被删除的那一项
Unshift () and Shift ()
1. 数组.unshift() //在数组的最前面添加一个元素;2. 数组.shift()  //不需要参数;在数组的最前面删除一项;例:    var arr = [1,2,3];    aaa = arr.unshift("abc");//在数组的最前面添加一个元素;    console.log(arr);//元素被修改了    console.log(aaa);//返回值是数组的长度;    aaa = arr.shift();//不需要参数;在数组的最前面删除一项;    console.log(arr);//元素被修改了    console.log(aaa);//被删除的那一项
Array element sort reverse ()
reverse()   //翻转数组例:    var arr1 = [1,2,3,4,5];    var aaa = arr1.reverse(); // [5,4,3,2,1]
Sort ()
sort() // 数组中元素排序;(默认:从小到大)      //  默认:按照首个字符的Unicode编码排序;如果第一个相同那么就比较第二个...例:            var arr = [4,5,1,3,2,7,6];    var aaa =arr.sort();    console.log(aaa);          // [1, 2, 3, 4, 5, 6, 7]    console.log(aaa === arr);// true 原数组被排序了(冒泡排序)    //默认还可以排列字母;    var arr2 = ["c","e","d","a","b"];    var bbb = arr2.sort();    console.log(bbb);         // ["a", "b", "c", "d", "e"]    console.log(bbb===arr2); // true 原数组被排序了(冒泡排序)sort() //数值大小排序方法,需要借助回调函数;例:      var arr = [4,5,1,13,2,7,6];      //回调函数里面返回值如果是:参数1-参数2;升幂;   参数2-参数1;降幂;      arr.sort(function (a,b) {        return a-b; //升序        //return b-a; //降序        //return b.value-a.value; //按照元素value属性的大小排序;      });      console.log(arr); // [1, 2, 4, 5, 6, 7, 13]
Sort () Underlying principle
    var aaa = bubbleSort([1,12,3], function (a,b) {//        return a-b;//实参:array[j]-array[j+1];        return b-a;//实参:array[j+1]-array[j];    });    console.log(aaa);    function bubbleSort(array,fn){        //外循环控制轮数,内循环控制次数,都是元素个数-1;        for(var i=0;i<array.length-1;i++){            for(var j=0;j<array.length-1-i;j++){//次数优化,多比较一轮,少比较一次;                //满足条件交换位置;//                if(array[j]>array[j+1]){//大于升幂排序;否则降幂;                //a-b>0  和  a>b是一个意思;                //b-a>0  和  a<b是一个意思;//                if(array[j]-array[j+1]>0){//升幂排序//                if(array[j+1]-array[j]>0){//降幂排序                //把两个变量送到一个函数中;                if(fn(array[j],array[j+1])>0){                    var temp = array[j];                    array[j] = array[j+1];                    array[j+1] = temp;                }            }        }        //返回数组        return array;    }
Operation of the array element concat ()
数组1.concat(数组2); // 链接两个数组;var arr1 = [1,2,3];var arr2 = ["a","b","c"];var arr3 = arr1.concat(arr2);console.log(arr3)   //    [1, 2, 3, "a", "b", "c"]
Slice ()
数组.slice(开始索引值,结束索引值);     //数组截取;例 :      var arr = [1, 2, 3, "a", "b", "c"];      console.log(arr.slice(3));            //从索引值为3截取到最后;["a", "b", "c"]      console.log(arr.slice(0,3));            //包左不包右;[1, 2, 3]      console.log(arr.slice(-2));            //负数是后几个;["b", "c"]      console.log(arr.slice(3,0));            //如果前面的比后面的大,那么就是[];[]      console.log(arr);                          //原数组不被修改;[1, 2, 3, "a", "b", "c"]
Splice ()
数组.splice(开始索引值,删除几个,替换内容1,替换内容2,...);  // 替换和删除;                                                      //改变原数组;返回值是被删除/替换的内容例:    var arr = [1,2,3,4,5,6,"a", "b", "c"]    arr.splice(5);        //从索引值为3截取到最后;(删除)    console.log(arr);     // [1, 2, 3, 4, 5]    arr.splice(1,2);    //(删除指定个数)从索引为1的开始删除2个    console.log(arr);   //[1, 4, 5]//替换    var arr = [1,2,3,4,5,6,"a", "b", "c"];    console.log(arr.splice(3,3,"aaa","bbb","ccc"));    //(删除指定数并替换)    console.log(arr);     // [1, 2, 3, "aaa", "bbb", "ccc", "a", "b", "c"]//    添加    arr.splice(3,0,"aaa","bbb","ccc");//(删除指定个数)//    console.log(arr);//截取或者替换之后的;   [1, 2, 3, "aaa", "bbb", "ccc", "aaa", "bbb", "ccc", "a", "b", "c"]
Indexof/lastindexof
数组.indexOf(元素);      // 给元素,查索引(从前往后)数组.lastIndexOf(元素);  // 给元素,查索引(从后往前)例:    var arr = ["a","b","c","d","c","b","b"];    console.log(arr.indexOf("b"));        // 1 查到以后立刻返回    console.log(arr.lastIndexOf("b"));    // 6 找到以后立刻返回    console.log(arr.indexOf("xxx"));    // -1;  查不到就返回-1;
Array iteration (traversal) every ()
对数组中每一项运行回调函数,如果都返回true,every返回true,如果有一项返回false,则停止遍历 every返回false;不写默认返回false像保镖失误一次,游戏结束!!!例:1.    var arr = [111,222,333,444,555];    arr.every(function (a,b,c) {        console.log(a);    //元素        console.log(b);    //索引值        console.log(c);    //数组本身;        console.log("-----");    //数组本身;        //数组中元素赋值:c[b] = 值;      a=有时候无法赋值;        return true;    });2.  //every返回一个bool值,全部是true才是true;有一个是false,结果就是false    var bool = arr.every(function (element, index, array) {        //判断:我们定义所有元素都大于200;        //if(element > 100){        if(element > 200){            return true;        }else{            return false;        }    })    alert(bool); //false
Filter ()
//   对数组中每一项运行回调函数,该函数返回结果是true的项组成的新数组//     新数组是有老数组中的元素组成的,return为ture的项;例:    var arr = [111,222,333,444,555];    var newArr = arr.filter(function (element, index, array) {        //只要是奇数,就组成数组;(数组中辨别元素)        if(element%2 === 0){            return true;        }else{            return false;        }    })    console.log(newArr); // [222, 444]
ForEach ()
// 和for循环一样;没有返回值;例:    var arr = [111,222,333,444,555];    var sum = 0;    var aaa = arr.forEach(function (element,index,array) {        console.log(element); // 输出数组中的每一个元素        console.log(index); // 数组元素对应的索引值        console.log(array); // 数组本身 [111, 222, 333, 444, 555]        sum += element; //数组中元素求和;    });    console.log(sum); // 数组元素加起来的和    console.log(aaa);//undefined;没有返回值 所以返回undefined
Map ()
//  对数组中每一项运行回调函数,返回该函数的结果组成的新数组//    return什么新数组中就有什么; 不return返回undefined; 对数组二次加工例:    var arr = [111,222,333,444,555];    var newArr = arr.map(function (element, index, array) {        if(index == 2){            return element; // 这里return了 所以下面返回的值是333        }        return element*100; // 返回的元素值都乘上100后的值    })    console.log(newArr); // [11100, 22200, 333, 44400, 55500]
Some ()
//对数组中每一项运行回调函数,如果该函数对某一项返回true,则some返回true; 像杀手,有一个成功,就胜利了!!!例:    var arr = [111,222,333,444,555];    var bool = arr.some(function (ele,i,array) {        //判断:数组中有3的倍数        if(ele%3 == 0){            return true;        }        return false;    })    alert(bool); //true ; 有一个成功就是true
Array empty
    1. arr.length = 0; // (不好,伪数组无法清空)    2. arr.splice(0); // 伪数组没有这个方法;    3. arr = [];     // 可以操作伪数组; (推荐!)// 伪数组: 就是长的像数组,但是没有数组的方法;也不能添加和删除元素;例: // arguments        fn(111,222,333);        function fn(){            arguments.length = 0; // 无法清空 返回 [1, 2, 3]           arguments.splice(0); // 会报错 arguments.splice is not a function            arguments = []; // 可以清空,返回空数组[]             console.log(arguments);        }
Array case
1. Output a string array to the form of a | split, such as "Liu Bei | Zhang Fei | guan Yu".       Use two ways to implement var arr = ["Liu Bei", "Zhang Fei", "Guan Yu"];       var separator = "|";       accumulate var str = arr[0] via for loop;       for (var i=1;i<arr.length;i++) {str + = Separator+arr[i]; } console.log (str);       Liu Bei, Zhang Fei | Guan Yu//join () can link the elements in the array into strings; Console.log (Arr.join ("|")); Liu Bei | Zhang Fei | guan Yu 2. Reverses the order of the elements of a string array. ["A", "B", "C", "D"], "D", "C", "B", "a"]. It is implemented in two ways.           Hint: The first and length-i-1 are exchanged//arrays. Reverse () method var arr = ["A", "B", "C", "D"]; Console.log (Arr.reverse ());  ["D", "C", "B", "a"]//Three kinds: 1. Forward traversal, reverse add;   2. Reverse traversal, positive add;           3. Meta-array element exchange location;               for (Var i=0;i<arr.length/2;i++) {var temp = arr[i];               Arr[i] = arr[arr.length-1-i];           Arr[arr.length-1-i] = temp; } console.log (arr); 3. Array of Wages [1500, 1200, 2000, 2100, 1800], the deletion of wages over 2000 var arr = [1500, 1200, 2000, 2100, 1800]   ;   Use filter () to form an array; return true; an array of components; var newArr = Arr.filter (function (Ele, I, array)       {//2000 above returns false;       if (ele<2000) {return true;       }else{return false;   }   }); Console.log (NEWARR); [1500, 1200, 1800]4.   ["C", "a", "Z", "a", "X", "a"] find the position of each of the occurrences of a in the array var arr = ["C", "a", "Z", "a", "X", "a"];   Traversal Array (for/while/do...while) ForEach (); Arr.foreach (function (ele, index, array) {//If the element equals "a" then output index value; if ("a" = = = Ele) {console.log (index)       ;   }}); 5. Write a method to remove duplicate elements of an array (array deduplication) var arr = ["Naruto", "Naruto", "adjuvant", "adjuvant", "Sakura", "Sakura"];       Method 1: The idea: Define a new array, traverse the old array, and judge if the new array contains no old array elements to add, otherwise do not add; var newArr = [];           Iterate over the old array Arr.foreach (function (Ele,index,array) {//detects the elements in the old array, if the new array exists it is not added, does not exist to add;           if (Newarr.indexof (ele) = = = 1) {//does not exist to add, (go to the new array to find the element index value, if 1 is not) Newarr.push (ele);       }       }); Console.log (NEWARR); ["Naruto", "adjuvant", "Sakura"]

1190000012276002

Array manipulation in JS

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.