JS对象 - Array属性要领汇总

属性名形貌
prototype为对象增加属性、要领
constructor返回数组对象援用
length返回数组元素数量
要领名形貌返回变动原数组
concat()衔接多个数组衔接后新数组N
join(”)将数组中一切元素合为一个字符串。按分开符分别兼并后新数组N
toString()数组转换为字符串( 与无参join雷同,逗号衔接)转换后字符串N
pop()删除数组末了一个元素(栈顶)删除的元素值Y
push()向数组的末端增加一个/多个元素新数组长度Y
shift()删除数组第一个元素删除的元素值Y
unshift()向数组的开首增加一个/多个元素新数组长度Y
reverse()倒置数组中元素的递次倒序后数组Y
slice(start,end)截取从start到end子数组(end省略为数组末端)截取子数组N
splice()(start,length,item1,item2,…)删除元素并增加新元素删除子数组Y
sort()对数组的元素举行排序(可自定规律)排序后数组Y
valueOf()返回 Array 对象的原始值Array对象N
indexOf()返回元素在数组中初次涌现的位置(严厉婚配===)下标/-1N

建立 Array 对象

new Array();
new Array(size);
new Array(element0, element1, ..., elementn);
var arr = []; //字面量体式格局

经测试用字面量体式格局跟new Array()耗时差不多,运用习气的差异吧~ new Array()能初始化长度~

属性

prototype 向对象增加属性和要领

function em(A,B)
{
    this.A = A;
    this.B = B;
}
var e = new em("AA","BB");
em.prototype.C = null;
e.C = 20000;
console.log(e.C); //20000
console.log(e); //em {A:"AA" , B:"BB" , C : 20000 }

constructor 返回对建立此对象的数组函数的援用

function employee(name,job)
{
    this.name = name;
    this.job = job;
}
var bill = new employee("Bill");
console.log(typeof bill.constructor); // function
console.log(bill.constructor); //function employee(name){this.name=name;this.job=job;}

要领

concat() 衔接两个或更多的数组(返回被衔接数组的副本)

var arr = [1, 2, 3];
var arr1 = [6, 7];
console.log(arr.concat(4, 5, arr1)); //[1, 2, 3, 4, 5, 6, 7]

join() 把数组的一切元素放入一个字符串,元素经由过程指定的分开符分开。

var arr = ['item 1', 'item 2', 'item 3'];
var list = '<ul><li>' + arr.join('</li><li>') + '</li></ul>'; //'<ul><li>item 1</li><li>item 2</li><li>item 3</li></ul>'

toString() 数组转换为字符串(与无参的 join()要领返回的串雷同)

var arr = new Array(3)
arr[0] = "A"
arr[1] = "B"
arr[2] = "C"
console.log(arr.toString()); //"A,B,C"

pop() 删除数组的末了一个元素,数组长度减 1,并返回被删除的元素的值

若数组为空,则pop()返回undefined值

var arr = ["A", "B", "C"];
console.log(arr); //["A", "B", "C"]
console.log(arr.pop()); //"C"
console.log(arr); //["A", "B"]

push() 向数组的末端增加一个或更多元素,并返回新的长度

var arr = ["A", "B", "C"];
console.log(arr); //["A", "B", "C"]
console.log(arr.push('D')); //4
console.log(arr); ////["A", "B", "C" , "D"]

reverse() 倒置数组中元素的递次

var arr = ["A", "B", "C"];
arr.reverse();
console.log(arr);// ["C", "B", "A"]

slice(start,end) 从数组中返回选定的子数组

var arr = ["A", "B", "C"];
// 从第一个元素最先截取到数组末端
console.log(arr.slice(1)); //["B", "C"]
console.log(arr); //["A", "B", "C"]

splice() 删除元素,并向数组增加新元素
(直接对数组操纵)

① 删除指定局限的数组元素:

var arr = new Array(6);
    arr[0] = "A"; 
    arr[1] = "B";
    arr[2] = "C";
    arr[3] = "D";
    arr[4] = "E";
    arr[5] = "F";
console.log(arr); //["A", "B", "C", "D", "E", "F"]
// 删除第三个元素今后的三个数组元素(包括第三个元素)
console.log(arr.splice(2, 3));  //["C", "D", "E"]
console.log(arr); //["A", "B", "F"]

② 从指定下标最先插进去指定元素(元素个数不限):

    var arr = new Array(6);
    arr[0] = "A"; 
    arr[1] = "B";
    arr[2] = "C";
    arr[3] = "D";
    arr[4] = "E";
    arr[5] = "F";
console.log(arr); //["A", "B", "C", "D", "E", "F"]
console.log(arr.splice(2, 0, "AA","BB")); // []
console.log(arr);//["A", "B", "AA" , "BB" , "C", "D", "E", "F"]

③ 删除指定局限的数组元素,并用指定元素替代(元素个数不限):

var arr = new Array(6);
    arr[0] = "A"; 
    arr[1] = "B";
    arr[2] = "C";
    arr[3] = "D";
    arr[4] = "E";
    arr[5] = "F";
console.log(arr);//["A", "B", "C", "D", "E", "F"]
console.log(arr.splice(2, 1, "AA","BB")); // ["C"]
console.log(arr); //["A", "B", "AA", "BB", "D", "E", "F"]

sort() 对数组的元素举行排序
对数组的援用

数组在原数组上举行排序,不天生副本。该要领默许是根据字符编码(ASCII)的递次举行排序的

var arr = new Array(3);
    arr[0] = "D";
    arr[1] = "S";
    arr[2] = "A";
console.log(arr); //["D", "S", "A"]
console.log(arr.sort()); //["A", "D", "S"]

自行指定排序划定规矩。以下:

var arr = new Array(6);
    arr[0] = 10;
    arr[1] = 5;
    arr[2] = 40;
    arr[3] = 25;
    arr[4] = 1000;
    arr[5] = 1;
console.log(arr); //[10, 5, 40, 25, 1000, 1]
//升序
console.log(arr.sort(function (a, b) {return a - b;}));// [1, 5, 10, 25, 40, 1000]

valueOf() 返回响应的原始值

var arr = new Array(2);
    arr[0] = 10
    arr[1] = 5
console.log(arr); //[10, 5]
console.log(typeof arr.valueOf()); //object
console.log(arr.valueOf()); //[10, 5]

shift() 删除并返回数组的第一个元素

var arr = ["A", "B", "C"];
console.log(arr); //["A", "B", "C"]
console.log(arr.shift()); //A
console.log(arr); //["B", "C"]

unshift() 向数组的开首增加一个或更多元素,并返回新的长度

var arr = ["A", "B", "C"];
console.log(arr); //["A", "B", "C"]
console.log(arr.unshift("AA")); //4
console.log(arr);//["AA", "A", "B", "C"]

indexOf() 返回元素在数组中涌现位置

额 之前一向认为只要string有这个要领… 补上补上

var arr = ['orange', '2016', undefined , null ,NaN];
console.log(arr.indexOf('orange')); //0
console.log(arr.indexOf('o')); //-1
console.log(arr.indexOf('2016'));  //1
console.log(arr.indexOf(2016));  //-1
console.log(arr.indexOf('undefined'));  //-1
console.log(arr.indexOf(null));  //3
console.log(arr.indexOf(NaN));  //-1

以上只是为了论证这里的indexOf运用的是严厉婚配(===),不会像string的indexOf一样隐式转换成字符串举行婚配~

    原文作者:zimoon
    原文地址: https://segmentfault.com/a/1190000008832224
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞