The method of JavaScript arrays | Share Study Notes

Array

An array of four common method

push()

  • The method may add one or more elements to the end of the array, and returns the new length of the array
  • Element may be passed as a parameter to be added to the method, these elements will be automatically added to the end of the array

pop()

  • This method can remove the last element of the array, and returns the removed element

unshift()

  • Add one or more elements to the beginning of the array, and returns the new length of the array
  • After you add an element, other elements of the index will be adjusted accordingly

shift()

  • You can delete the first element of the array, and returns it as the return value

Iterate

  • for loop
var arr = ["张三","李四","王五","马六"];

for(var i=0;i<arr.length;i++){
    console.log(arr[i]);
}
  
  • forEach *
//forEach()  不支持IE9以下浏览器
var arr = ["张三","李四","王五","马六"];

/*
 * forEach()方法需要一个函数作为参数
 *  - 像这种函数,由我们创建但不是我们调用的,我们称之为回调函数
 *  - 数组中有几个元素就会执行几次,每次执行时,浏览器会将遍历到的元素以实参的形式
 *      传递进来,我们可以定义形参,来读取这些内容
 *  - 浏览器会在回调函数中传递三个参数:
 *      第一个参数:当前正在遍历的元素
 *      第二个参数:当前正在遍历的元素的索引
 *      第三个参数:当前正在遍历的数组
 */

arr.forEach(function(value, index, obj){
    console.log(value);
});

slice (), and splice ()

slice()

  • It can be used to extract elements from the specified array
  • Parameters: index of the clipping position, end position taken index (before and after the opening and closing, the second argument can not write, take a default to the last element)
  • Index may be the last element of negative -1

splice()

  • Remove elements add a new element to the array
  • The specified element is removed from the original array and returns
  • Parameters:
    First: Start position index
    second: The number of deleted
    : The third and subsequent
    Before you can pass some new elements that will be automatically inserted at the beginning index position

Deduplication Array

//创建一个数组
var arr = [1,2,3,2,1,3,4,2,5];

//数组去重1 
//获取数组中的每一个元素(遍历)
for(var i=0; i<arr.length; i++){
    //遍历当前元素后的所有元素
    for(var j=i+1; j<arr.length; j++){
        //判断两个元素值是否相等
        if(arr[i]==arr[j]){
            //相等则证明出现重复,删除j对应元素
            arr.splice(j,1);
            //当删除了当前j所对应元素后,后面的元素会自动补位
            //导致j新对应的元素无法进行比较,如果该元素依旧重复则会遗漏
            //故使j自减
            j--;
        }
    }
}

console.log(arr);

An array of other methods

/*  1
 *  concat()可以连接两个或多个数组,并将新的数组返回
 *  - 该方法不会对原数组产生影响
 *  - 参数也可以为元素
 */

/*  2
 *  join()方法可以将数组转换为一个字符串
 *  - 该方法不会对原数组产生影响,而是将转换后的结果返回
 *  - 可以指定一个字符串作为参数,它将会作为数组元素的连接符,不填则默认为","
 */

/*  3
 *  reverse()方法可以用来反转数组
 *  - 会直接操作原数组  
 */

/*  4
 *  sort()用来对数组元素进行排序
 *  - 改变原数组
 *  - 默认按照Unicode编码进行排序,即使对纯数字数组排序一会按照Unicode编码排序
 *  - 我们可以自己指定排序规则,在sort()添加一个回调函数
 *      回调函数中需要定义两个形参
 *      浏览器将会分别使用数组中的元素作为实参去调用回调函数
 *  - 浏览器会根据回调函数的返回值来决定元素的顺序
 *      如果返回一个大于0的值,则元素交换位置
 *      如果返回一个小于0的值,则元素位置不变
 *      如果返回一个0,则认为两个元素相等,不交换位置
 */

var arr = [5,4,2,3,1];

arr.sort(function(a,b){
    return a - b;   //升序
    //return b - a; //降序
});

Guess you like

Origin www.cnblogs.com/meow999/p/12064434.html