Array - 最常用的js对象之一
Posted spikeeee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Array - 最常用的js对象之一相关的知识,希望对你有一定的参考价值。
数组对象: Array
1. 前言
数组也好, 列表也好, 在不同的编程语言里面叫法虽然不一样, 但其本质是一样的, 作为最常用的对象, 其属性和方法是必须要牢记在心的, 以下是Array对象的完整属性方法示例.
2. 参数
情况1: 没参数
var arr = new Array(); // 实例化时括号中不加参数, 返回一个空数组
console.log(arr); // 输出: []
情况2: 有数字参数
var arr = new Array(5); // 括号中的10代表实例化的数组中的元素个数;
console.log(arr); // 输出: [null,null,null,null,null]
console.log(arr.length); // 输出: 5
情况3: 有一系列数据参数
var arr = new Array(‘A‘,"B",10,[1,2,3]);
console.log(arr); // 输出: [‘A‘,"B",10,[1,2,3]]
console.log(arr.length); // 输出: 4
3. 属性
constructor: 返回对创建此对象的数组函数的引用
var arr = new Array();
console.log(arr.constructor); // 返回: [object Function]
document.write(arr.constructor); // 返回: function Array() { [native code] }
length: 返回数组的长度
var arr = new Array(1,2,3);
console.log(arr.length); // 3
prototype: 有能力向对象添加属性和方法
var arr = new Array();
Array.prototype.name = "雷棠";
console.log(arr.name); // 雷棠;
4. 方法
Array.concat(): 数组拼接
var arr = new Array("hello");
console.log(arr.concat(["world"])); // ["hello", "world"]
console.log(arr.concat("world","!")) // ["hello","world","!"]
Array.join(): 数组转字符串
var arr = new Array(1,2,3,4);
console.log(arr); // [1,2,3,4]
console.log(arr.join()); // ‘1,2,3,4‘
console.log(arr.join("-")); // ‘1-2-3-4‘
Array.pop(): 删除数组的末尾元素
var arr = new Array("A");
console.log(arr.pop()); // ‘A‘
console.log(arr.length); // 0
console.log(arr.pop()); // undefined
console.log(arr.length); // 0
Array.push(): 往数组末尾添加元素
var arr = new Array("A");
console.log(arr); // "A"
console.log(arr.length); // 1
console.log(arr.push("B","C","D")); // 4
console.log(arr); // ["A","B","C","D"]
console.log(arr.length); // 4
Array.reverse(): 倒序数组
var arr = new Array("A","B","C");
console.log(arr.reverse()); // ["C","B","A"]
console.log(arr); // ["C","B","A"]
Array.shift(): 删除数组的首个元素
var arr = new Array("A","B");
console.log(arr.shift()); // "A"
console.log(arr); // ["B"]
console.log(arr.shift()); // "B"
console.log(arr); // []
console.log(arr.shift()); // undefined
console.log(arr); // []
Array.slice(): 从已有数组中返回选定的元素
var arr = new Array("A","B","C","D","E");
console.log(arr.slice()); // ["A","B","C","D","E"]
console.log(arr.slice(1)); // ["B","C","D","E"]
console.log(arr.slice(1,3)); // ["B","C"]
console.log(arr.slice(end=3)); // ["D","E"]
console.log(arr.slice(-1,-2)); // []
console.log(arr.slice(-2,-1)); // ["D"]
console.log(arr); // ["A","B","C","D","E"]
Array.sort(): 数组元素排序
var arr1 = new Array(1,3,2,4,6,5);
console.log(arr1.sort()); // [1,2,3,4,5,6]
console.log(arr1); // [1,2,3,4,5,6]
var arr2 = new Array("1","3","2","4","6","5");
console.log(arr2.sort()); // ["1","2","3","4","5","6"]
console.log(arr2); // ["1","2","3","4","5","6"]
var arr3 = new Array("B","d","A","c","C","D");
console.log(arr3.sort()); // ["A","B","C","D","c","d"]
console.log(arr3); // ["A","B","C","D","c","d"]
Array.splice(): 删除并添加新元素
var arr = new Array("A","B","C");
console.log(arr.splice(2,1)); // ["C"]
console.log(arr); // ["A","B"]
console.log(arr.splice(1,0,"C")); // []
console.log(arr); // ["A","C","B"]
Array.toSource(): 返回对象源代码
// 注意: 只有gecko内核的浏览器支持此方法
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
document.write(bill.toSource()); // ({name:"Bill Gates", job:"Engineer", born:1985})
Array.toString(): 数组转字符串并返回结果
var arr = new Array(1,2,3,4);
console.log(arr.toString()); // "1,2,3,4"
Array.toLocalString(): 转换为本地数组并返回
var arr = new Array(1,2,3,4);
console.log(arr.toLocaleString()); // "1,2,3,4"
Array.unshift(): 向数组开头添加一个或多个数组并返回新的长度
// 注意: 在IE中无法正常工作
var arr = new Array(0,1,2);
console.log(arr.unshift(-1,-2)); // 5
console.log(arr); // [-1,-2,0,1,2]
console.log(arr.unshift()); // 5
Array.valueOf(): 返回数组对象原始值
var arr1 = new Array(1,2,3);
console.log(arr1.valueOf()); // [1,2,3]
5. 结论
OK, 这是目前收集到的所有Array对象的属性和方法的使用示例, 都是非常基础和简单的, 在初期应该会经常翻阅.
以上是关于Array - 最常用的js对象之一的主要内容,如果未能解决你的问题,请参考以下文章