数组的操作方法大全

数组的操作方法

数组的查询

includes函数,函数的参数为查找的值,如果数组中有被查找的值,则函数输出“true”

var movePos=[11,22];
movePos.includes(11)//true
movePos.includes(33)//false

indexOf函数,

方法arr.indexOf(find,start);
find:要找的内容,必须;

start:查找开始下标,可选;

返回:查找返回数据所在的下标,如果没找到,返回-1
如果只要知道是否有8这个数字,直接调用arr.indexOf(8),如果返回值不为-1,说明找到了

var s=[1,2,3,5,6,9];
var u=s.indexOf(5);//返回5所在的下标3
var d=s.indexOf(8);//返回-1

find函数,

find()方法返回第一个满足过滤方法的元素,一个都没有满足的就返回undefined,遇到一个满足的元素后遍历就停止了

const arr = [{name:“Jim”,age:“20”},{name:“Lily”,age:“18”},{name:“Mei”,age:“18”},]
arr.find((item)=>{
return item.age == ‘18’
})
//{name:“Lily”,age:“18”}

数组的添加和删除方法

shift() 方法:把数组的第一个元素删除,并返回第一个元素的值

var movePos=[11,22];
movePos.shift()
console.log(movePos)//[22]
alert(movePos)//22

document.write(movePos.length);//1

concat() 方法:用于连接两个或多个数组,并返回一个新数组,新数组是将参数添加到原数组中构成的

var movePos=[11,22];

var arr=movePos.concat(4,5);
console.log(arr);//[11, 22, 4, 5]
alert(arr);//11, 22, 4, 5

var ar1=[2,3]
var ar3=[1111,2222];
var ar2=arr.concat(ar1,ar3);
console.log(ar2); //[11, 22, 4, 5, 2, 3, 1111, 2222]
alert(ar2); //11, 22, 4, 5, 2, 3, 1111, 2222

document.write(arr.length);//4

document.write(ar2.length);//8

join() 方法:用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的。

返回一个字符串。该字符串是通过把 arrayObject 的每个元素转换为字符串,然后把这些字符串连接起来,在两个元素之间插入separator 字符串而生成的。

var movePos=[11,22];

var arr=movePos.join("+");
document.write(arr) //11+22

pop() 方法:用于删除并返回数组的最后一个(删除元素)元素,如果数组为空则返回undefined ,把数组长度减 1

var movePos=[11,22,33];
var arr=movePos.pop();
document.write(arr) //33

document.write(arr.length)//2

push() 方法:可向数组的末尾添加一个或多个元素,并返回新的长度,(用来改变数组长度)。

var movePos=[11,22];

var arr=movePos.push(“333”);

document.write(arr) //返回的结果就是数组改变后的长度:3

document.write(arr.length) //undefined

reverse() :方法用于颠倒数组中元素的顺序。

var movePos=[11,22];

var arr=movePos.reverse();

document.write(arr) //返回新的数组:22,11

document.write(arr.length) //返回数组长度2

slice() 方法:可从已有的数组中返回选定的元素。slice(开始截取位置,结束截取位置)

var movePos=[11,22,33];

var arr=movePos.slice(1,2);

document.write(arr) //返回截取的元素:22

document.write(arr.length) //返回数组长度1,截取的数组的长度

splice() :方法向/从数组中添加/删除项目,然后返回被删除的项目。

var movePos=[11,22,33,44];

var arr=movePos.splice(1,2);//movePos.splice(开始删除的下表位置,删除数组元素的个数);

document.write(arr) ; //返回新的数组:22,11

document.write(arr.length) ;//返回数组长度2

splice() 方法可删除从 index 处开始的零个或多个元素,并且用参数列表中声明的一个或多个值来替换那些被删除的元素。

var movePos =[111,222,333,444];

movePos.splice(2,1,"666")//movePos.splice(开始删除的下表位置,删除数组元素的个数,向数组添加的新项目。);从下标2开始删除一位,并用666替换删除下表位置的元素
document.write(movePos + "<br />")

unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。

unshift:将参数添加到原数组开头,并返回数组的长度

var movePos =[111,222,333,444];
movePos.unshift(“55555”)
document.write(movePos + “
”)//55555,111,222,333,444

sort(orderfunction):按指定的参数对数组进行排序

var movePos =[“444”,“111”,“333”,“222”];

movePos.sort(1)

document.write(movePos + “
”)//55555,111,222,333,444

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值