常用数组Array

fill(value,start,end)将数组内的值替换

var array1 = [1, 2, 3, 4];
//从第二位开始到第四位用0代替
console.log(array1.fill(0, 2, 4));
// result: [1, 2, 0, 0]

//从第一位开始用5代替
console.log(array1.fill(5, 1));

// result: [1, 5, 5, 5]

//用6替换数组元素
console.log(array1.fill(6));
// result: [6, 6, 6, 6]

unshift()添加元素到数组第一位

let name=["ylj","zhq","htt"];

let popName=name.unshift("sll");

console.log(name);//result:

["sll","ylj","zhq","htt"];

push()添加元素到数组最后一位

let name=["sll","ylj","zhq"];

let popName=name.push("htt");

console.log(name);//result:

["sll","ylj","zhq","htt"];

shift()删除数组第一位

let name=["sll","ylj","zhq","htt"];

let popName=name.shift();

console.log(name);//result:

["ylj","zhq","htt"]

pop()删除数组最后一位

let name=["sll","ylj","zhq","htt"];

let popName=name.pop();

console.log(name);//result:["sll","ylj","zhq"];

splice()在数组任意位置添加或删除元素

var name=["sll","ylj","zhq"];

name.splice(2,0,'htt');//在索引为2的位置前插入元素'htt'

console.log(name);//["sll","ylj","htt","zhq"];

name.splice(2,1);//在索引为2的位置前删除元素

console.log(name);//["sll","ylj","zhq"];

concat()多个数组合并

var array1 = ['a', 'b', 'c'];
var array2 = ['d', 'e', 'f'];
console.log(array1.concat(array2));
// 输出: Array ["a", "b", "c", "d", "e", "f"]

sort()排序

var name=["sll","ylj","zhq","htt"];

name.sort();

console.log(name);//["htt","sll","ylj","zhq"]

var num=[11,3,8,29,13,5];

console.log(num);//[3,5,8,11,13,29]

reverse()倒序

var num=[11,3,8,29,13,5];

num.reverse();

console.log(num);//[5,13,29,8,3,11]

join()数组分割成字符串,默认以逗号(,)分割

let name=["sll","ylj","zhq","htt"];

console.log(name.json());//'sll,ylj,zhq,htt'

console.log(name.json(""));//'sllyljzhqhtt'

console.log(name.json("-"));//'sll-ylj-zhq-htt'

split()字符串分割成数组


slice()抽取当前数组中的一段元素组合成一个新数组,从开始到结束

var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
//输出: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// 输出: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// 输出: Array ["bison", "camel", "duck", "elephant"]

toSource()查看数租,一般不推荐使用

let name=new Array("sll","ylj","zhq","htt");

name.toSource();//["sll","ylj","zhq","htt"];

indexof()返回数组中元素的索引值,不存在返回-1

 arr=[2,4,8];

arr.indexOf(2);//0

arr.indexOf(7);//-1

arr.indexOf(8,2);//2

arr.indexOf(2,-1);//-1

arr.indexOf(2,-3);//0

forEach()数组中愿所有执行一次回调函数

语法:

array.forEach(callback(currentValue, index, array){
    //do something
}, this)

array.forEach(callback[, thisArg])

const arr=['a','b','c'];

arr.forEach(function(ele){

    console.log(ele);

});

arr.forEach(ele==>console.log(ele));

//a

//b

//c

some()测试函数(至少有一个满足),返回true或false

const bigger=(ele,index,arr)=>{

    return ele>10;

}

[2,5,8,1,4].some(bigger);//false

[12,5,8,1,4].some(bigger);//true

every()测试函数(每个元素都要满足),返回true或false

function bigger=(ele,index,arr){

    return (ele>=10);

}

var pass=[2,5,8,1,30].every(bigger);//false

[12,50,80,10,14].some(bigger);//true

filter()过滤

function bigger=(ele){

    return ele>=10;

}

var filtered=[12,5,8,1,30].filter(bigger);//[12,30]

find()满足测试的元素并返回值,找不到返回undefined

function bigger=(ele){

    return ele>=10;

}

var res=[1,5,8,1,30].find(bigger);//30

findIndex()满足测试的元素并返回元素索引值,找不到返回-1

function bigger=(ele){

    return ele>=10;

}

var res=[1,5,8,1,30].findIndex(bigger);//4

reduce()数组元素从左至右减少为单个值

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// 输出: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// 输出: 15

reduceRight()数组元素从右至左减少为单个值

let  a=[

[0,1],

[2,3],

[4,5]

].reduceRight((a,b)=>{

    return a.concat(b);

},[]);//[4,5,2,3,0,1]

可参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值