数组方法-数组API

数组API

1.数组序列化

toString()

默认情况下以逗号分隔字符串的形式返回数组项

var arr = [1,5,2,8,10,{a:1}];
console.log(arr);//[ 1, 5, 2, 8, 10, { a: 1 } ]
console.log(arr.toString());//”1,5,2,8,10,[object Object]”

join()

使用指定字符串来分隔数组字符串

var arr = [1,5,2,8,10,{a:1}];
console.log(arr);//[ 1, 5, 2, 8, 10, { a: 1 } ]
console.log(arr.join("-"));//”1-5-2-8-10-[object Object]”

2.构造函数方法

Array.isArray()

用来判断某一个变量是否是一个数组对象

var a = [1,3,'wqe'];
console.log(Array.isArray(a));
//输出true

Array.from()

从类数组对象或者可迭代的对象中创建一个新的数组实例

var myArr = Array.from('briup');
console.log(myArr);
//输出[ 'b', 'r', 'i', 'u', 'p' ]

Array.of()

根据一组参数来创建新的数组实例,支持任意参数数量和类型

var myArr = Array.of('briup',12,[],'',null,undefined,{qwe:123});
console.log(myArr);
//输出 [ 'briup', 12, [], '', null, undefined, { qwe: 123 } ]

3.栈与队列方法

Array.prototype.push()

向数组的末尾添加一个或多个元素,并返回新的长度改变数组长度

var myArr = ["Banana", "Orange", "Apple"];
myArr.push("Mango",123,[],'',{w:123});
console.log(myArr);
// 输出 [ 'Banana', 'Orange', 'Apple', 'Mango', 123, [], '', { w: 123 } ]

Array.prototype.pop()

用于删除数组的最后一个元素返回删除的元素改变数组长度

var myArr = ['briup',12,[],'',null,undefined,{qwe:123}];
myArr.pop();
console.log(myArr);
// 输出 [ 'briup', 12, [], '', null, undefined ]

Array.prototype.shift()

用于删除数组的的第一个元素返回删除的元素改变数组长度

var myArr = ['briup',12,[],'',null,undefined,{qwe:123}];
myArr.shift();
//console.log(myArr());//briup
console.log(myArr);
// 输出 [ 12, [], '', null, undefined, { qwe: 123 } ]

Array.prototype.unshift()

向数组的开头添加一个或多个元素,返回新的长度改变数组长度

var myArr = ['briup',12,[],''];
myArr.unshift(null,undefined,{qwe:123});
//console.log(myArr.unshift(null,undefined,{qwe:123})); //7
console.log(myArr);
//输出 [ null, undefined, { qwe: 123 }, 'briup', 12, [], '' ]

4.排序方法

Array.prototype.reverse()

用于颠倒数组中元素的顺序 返回颠倒后的数组

var myArr = ['briup',12,[],'',null,undefined,{qwe:123}];
myArr.reverse();
//console.log(myArr.reverse());
//[ { qwe: 123 }, undefined, null, '', [], 12, 'briup' ]
console.log(myArr);
//输出 [ { qwe: 123 }, undefined, null, '', [], 12, 'briup' ]

Array.prototype.sort()

用于对数组的元素进行排序,如果调用该方法时没有使用参数,将按字母顺序(Ascall编码)对数组中的元素进行排序 默认排序顺序为按字母升序。

var fruits = ["Banana", "Orange", "Apple","apple","Apppp",1,12];
fruits.sort();
//console.log(fruits.sort());
//[ 1, 12, 'Apple', 'Apppp', 'Banana', 'Orange', 'apple' ]
console.log(fruits)
//输出 [ 1, 12, 'Apple', 'Apppp', 'Banana', 'Orange', 'apple' ]
数字排序(升序)
var arr = [40,100,1,5,25,10];
arr.sort(function(a,b){
	return a-b
});
console.log(arr);
//arr输出结果:1,5,10,25,40,100
数字排序(降序)
var arr = [40,100,1,5,25,10];
arr.sort(function(a,b){
	return b-a
});
console.log(arr);
//arr输出结果:100,40,25,10,5,1

5.操作方法

Array.prototype.concat()

用于连接两个或多个数组,不会改变现有数组返回新的数组

var myArr = ['briup',12];
var arr1 = [[],'',null];
var arr2 = [undefined,{qwe:123}]
var arr3 = 'qwee'
var newArr = myArr.concat(arr1,arr2,arr3);
//console.log(myArr); //[ 'briup', 12 ]
console.log(newArr);
//输出
[ 'briup', 12, [], '', null, undefined, { qwe: 123 }, 'qwee' ]

Array.prototype.slice()

用于提取字符串的某个部分,返回一个新的数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素。不改变原数组

var arr = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; 
var result = arr.slice(1,3);
//输出 [ 'Orange', 'Lemon' ]

Array.prototype.splice()

用于添加或删除数组中的元素,改变原始数组

语法
array.splice(index,howmany,item1,.....,itemX)
//array.splice(规定从何处添加/删除元素,应该删除多少元素,要添加到数组的新元素);
向数组中添加新元素
var fruits = ["Banana", "Orange", "Apple", "Mango"];
//从数组索引位置 2 开始,不删除元素,添加两个新的元素"Lemon","Kiwi"
fruits.splice(2,0,"Lemon","Kiwi");
console.log(fruits);
//输出 [ 'Banana', 'Orange', 'Lemon', 'Kiwi', 'Apple', 'Mango' ]
移除数组的第三个元素,并在数组第三个位置添加新元素:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,1,"Lemon","Kiwi");
console.log(fruits);
//输出 [ 'Banana', 'Orange', 'Lemon', 'Kiwi', 'Mango' ]

从第三个位置开始删除数组后的两个元素:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,2);
console.log(fruits);
//输出 [ 'Banana', 'Orange' ]

6.位置方法

Array.protoytpe.indexOf()

语法
array.indexOf(item,start)

返回数组中某个指定元素的位置,如果没有返回-1

var fruits = ["Banana", "Orange", "Apple", "Mango"]; 
var a = fruits.indexOf("Apple",1);
console.log(a);
//输出 2
//以上输出结果意味着 "Apple" 元素位于数组中的第 3 个位置。

Array.prototype.lastIndexOf()

语法
array.lastIndexOf(item,start)

返回一个指定的元素在数组中最后出现的位置,从该字符串的后面向前查找。如果要检索的元素没有出现,则该方法返回 -1。

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.lastIndexOf("Apple");
console.log(a);
//a输出结果:2
//以上实例输出结果意味着 "Apple" 位于数组中的第 2 个位置.

7.迭代方法

Array.prototype.every()

用于检测数组所有元素是否都符合指定条件(通过函数提供),不会对空数组进行检查,不改变原数组

有一个不符合 返回false 都符合 返回true

var arr = [12,31,21,34];
var result = arr.every(function(key){
    return key > 18
})
console.log(result);
// 结果 false

Array.prototype.some()

用于检测数组中的元素是否满足指定条件(函数提供),不会对空数组进行检测,不改变原数组

有一个满足 返回true 没有满足条件的元素 返回false

var arr = [12,31,21,34];
var result = arr.some(function(key){
    return key > 18
})
console.log(result);
// 结果 true

Array.prototype.filter()

创建一个新数组,新数组中的元素是通过检查指定数组中符合条件的所有元素,如果没有符合条件的元素则返回空数组,不会对空数组进行检测,不改变原数组

var arr = [12,31,21,34];
var result = arr.filter(function(key){
    return key > 18
})
console.log(result);
//输出 [ 31, 21, 34 ]

Array.prototype.map()

返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值,按原始数组元素顺序依次处理函数,不会对空数值进行检测,不改变原数组

var arr = [12,31,21,34];
var result = arr.map(function(key){
    return key > 18
})
console.log(result);
//结果 [ false, true, true, true ]

Array.prototype.forEach()

用于调用数组的每一个元素,并将元素返回给回调函数,对空数值不会执行回调函数

列出数组的每个元素:
var numbers = [4, 9, 16, 25];
numbers.forEach(function (number, index) {
  console.log(index + ' -- ' + number);
})
// 0 -- 4
// 1 -- 9
// 2 -- 16
// 3 -- 25
数组序列化 toString() join()
构造函数方法 isArray() from() of()
栈与队列方法 push() pop() shift() unshift()
排序方法 reverse() sort()
操作方法 concat() slice() splice()
位置方法 indexOf() lastIndexOf()
迭代方法 every() some() filter() map() forEach()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值