javaScript(九) 数组

]

songs[0]={id:1,name:“123”,singer:“abc”,score:“88”},通过赋值的方式等号左边的值赋值给了数组下标的位置

数组构造函数上的方法

**Array.isArray(arg):**检测传入的参数是否是数组,如果是数组,返回true,如果不是,返回false,示例:

array.isArray([])   //true

array.isArray({id:1})   //false

**Array.from(items,[,mapfn[,thisArg]]):**从一个伪数组或可迭代对象中创建一个新的数组实例,返回的是新生成的数组实例

item:想要转成数组的伪数组或可迭代对象

mapfn:回调函数,每个新数组均会执行,可选。这个回调函数有2个参数,一个是Item:数组中正在处理的元素,第二个是index:正在处理的当前元素的索引

thisArg:执行mapfin时的用于this的值,可选

示例1:

var b=Array.from(“163”);

console.log(b) //输出[‘1’,‘6’,‘3’]

示例2:

var ab={

1:“1”, //这边的属性名代表下标,只能是数字,而数字代表在数组中的位置,如果是name,obj这种英文属性名,则会在数组显示undefined

2:“2”,

3:“3”,

length:3 //必须,如果不写长度,数组变成空数组

}

console.log(Array.from(ab));

示例3:

var num=function (item,index) {

return parseInt(index) //item为数组中的每一个值,index为对应的下标,这例中,item为[“1”,“6”,“3”],通过parseInt()方法转成[1,6,3],index为[0,1,2]

}

console.log(Array.from(“163”,num)) //通过from方法将伪数组a转换成数组,然后将数组传入num方法,经过计算返回新数组

示例4:

var ac={

add:function (value) {

return parseInt(value)+2

},

opp:function (n) {

return this.add(n)

}

}

console.log(Array.from(“163”,ac.opp,ac))

解释:这边定义了一个对象,对象包含了两个属性,分别是两个对象函数,第一个函数add,返回了通过parseInt()方法将参数转成number类型,然后+2获得了一个新数字,第二个函数op,就是将参数传入该对象的add方法,

后面,通过数组Array的from方法调用了ac的对象函数并返回了新的数组,from一共往对象函数传入了3个参数,第一个字符串“163”,第二个回调函数ac.opp方法,第三个指定对象,先是,第一个参数“163”,被分解成为[“1”,“6”,“3”]传入第二个参数ac.opp方法,数组中的每一个值都回执行一次ac.opp方法,每一次执行都会将字符串转成数字,并经过add()方法+2返回,第三个参数指定对象,如果不指定对象,那么opp()方法中的this.add()中的this会默认指向window,那这时window本身没有add()方法,所以会报错

最终输出:[3,8,5]

Array.of(…item):根据输入参数生成新的数组,…item可以为任意数量的参数,最终会按照输入顺序返回数组实例,示例:

console.log(Array.of(1,“2”,[],{name:function () {},stit:“stit”}))

输出:[1,“2”,[],{name:function () {},stit:“stit”}]

数组实例的属性

arr.length

var songs=[

{id:1,name:“My”,singer:“aaa”,score:“90”},

{id:2,name:“123”,singer:“bbb”,score:“80”},

{id:2,name:“枷”,singer:“ccc”,score:“98”},

]

songs.length;   //输出:3,长度为3,也就是一共有3首歌,如果数组为空数组,那么length返回0

数组原型上的方法

Array.prototype.sort(comparefn)

通过sort()方法对数组进行排序,参数comparefn是一个函数**,里面规定了排序的规则**,最终根据函数的返回值进行计算,如果返回值>0,则a在b的后面,如果返回值小于0,则a在b的前面,如果等于0,则a,b的顺序不变,如果没有指定规则,那么sort()会将数组转成Unicode字符编码的排量数序显示,最终这个方法返回的是排序后的数组

var songsM=[

{id:1,name:“My”,singer:“aaa”,score:“90”},

{id:3,name:“123”,singer:“bbb”,score:“80”},

{id:2,name:“枷”,singer:“ccc”,score:“98”},

]

var socrea=function (a,b) {

return parseInt(a.score)-parseInt(b.score);

}

console.log(songsM.sort(socrea))

解释:输出一个将数组songsM排列后的新数组,规则是函数socrea,在这个函数里,获取了数组中对象的score值,并通过parseInt()方法转换成数字进行比较,最终形成按升序排列的数组,如果降序的话函数中的renturn 应该是parseInt(b.score)-parseInt(a.score),这样的会是降序排列

Array.prototype.reverse()

将数组中的位置进行颠倒后重新排序,这个方法没有参数,返回值是重新排序后的方法,例如:

var a=[“asd”,“2”,2]

a.reverse()

输出:[2,“2”,“asd”]

Array.prototype.push(…item)

将一个或者多个元素依次添加到数组结尾,…item为参数,可以是多个,返回值是数组的长度,也就是length值,另外原数组会被改变

var ad=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

console.log(ad.push(“1”,“3”,“2”))

console.log(ad)

第一个输出:6

第二个输出:[{id:1,name:“My”,score:“90”},{id:3,name:“123”,score:“80”},{id:2,name:“枷”,score:“98”} ,“1”,“3”,“2”]

Array.prototype.unshift(…item)

与push()方法对应,将一个或多个元素一次添加到数组开头,…item参数可以是多个,返回值是数组的长度,也就是length的值,另外原数组会被改变

var ae=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

console.log(ae.unshift(“1”,“3”,“2”))

console.log(ae)

第一个输出:6

第二个输出:[“1”,“3”,“2”,{id:1,name:“My”,score:“90”},{id:3,name:“123”,score:“80”},{id:2,name:“枷”,score:“98”} ]

Array.prototype.pop()

删除数组中的最后一个元素,该方法没有参数**,返回值是删除的那一个数组**,如果数组为空,那么会返回undefined

var af=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

console.log(af.pop())

console.log(af)

第一个输出:{id:2,name:“枷”,score:“98”}

第二个输出:[ {id:1,name:“My”,score:“90”}, {id:3,name:“123”,score:“80”} ]

Array.prototype.shift()

删除数组中的第一个元素,该方法没有参数,返回值是删除的那一个参数,如果数组为空,那么会返回undefined

var ag=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

console.log(ag.shift())

console.log(ag)

第一个输出:{id:1,name:“My”,score:“90”}

第二个输出:[  {id:3,name:“123”,score:“80”}, {id:2,name:“枷”,score:“98”} ]

Array.prototype.splice(index,deleteCount,…item)

-在任意位置删除或者添加元素

-参数

  • index:修改时开始的位置

  • deleteCount:要删除的个数,可选,如果为0,那么表示不删除

  • …item:要添加进数组的元素,可选,如果没有,那么表示仅仅用作删除

-返回值

  • 返回值是被删除或添加后的元素组成的新数组

示例1:

var ah=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

console.log(ah.splice(0,0,“1”,“2”,“3”));

console.log(ah)

第一个输出:[],因为删除的个数是0,那么splice()输出为一个空数组

第二个输出:[ “1”,“2”,“3”,{id:1,name:“My”,score:“90”}, {id:3,name:“123”,score:“80”}, {id:2,name:“枷”,score:“98”} ]

示例2:

var ah=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

console.log(ah.splice(1,1,“1”,“2”,“3”));

console.log(ah)

第一个输出:[{id:3,name:“123”,score:“80”}]

第二个输出:[{id:1,name:“My”,score:“90”}, “1”,“2”,“3”, {id:2,name:“枷”,score:“98”} ]

示例3:

var ah=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

console.log(ah.splice(1,1));

console.log(ah)

第一个输出:[{id:3,name:“123”,score:“80”}]

第二个输出:[{id:1,name:“My”,score:“90”}, {id:2,name:“枷”,score:“98”} ]

解释:这一种相当于删除某个索引位置的元素

Array.prototype.fill(value[,start[,end]])

-使用给定的值填充起始位置到结束位置之间的全部元素

-参数

  • value:用来填充数组的值(注意:用来填充的数组会循环填充每一个位置,并不是截取当中的某一段替换)

  • start:起始位置,可选,默认为0

  • end:结束位置,可选,默认为数组长度(例如结束位置是2,意思是到2前面,不包含2),如果是负数,那么从数组的结尾开始倒数算起

-返回值

  • 修改后的数组

示例1:

var ai=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

console.log(ai.fill({id:4,name:“32”,score:“32”},1,2));

console.log(ai)

第一个输出:[ {id:1,name:“My”,score:“90”},{id:4,name:“32”,score:“32”}, {id:2,name:“枷”,score:“98”} ]

第二个输出:[ {id:1,name:“My”,score:“90”},{id:4,name:“32”,score:“32”}, {id:2,name:“枷”,score:“98”} ]

示例2:

var ai=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

console.log(ai.fill({id:4,name:“32”,score:“32”},1,3));

console.log(ai)

第一个输出:[ {id:1,name:“My”,score:“90”},{id:4,name:“32”,score:“32”},{id:4,name:“32”,score:“32”}]

第二个输出:[ {id:1,name:“My”,score:“90”},{id:4,name:“32”,score:“32”},{id:4,name:“32”,score:“32”} ]

Array.prototype.copyWithin(target,start[,end])

-浅复制数组的一部分到同一个数组的另外一个位置

-参数

  • target:复制的目标位置,从数组的0位置开始算

  • start:复制的起始位置,可选,从数组的0位置开始算

  • end:复制的结束位置,可选,从数组的0位置开始算,注意不包括这个,是到这个的前一个

-返回值

  • 修改后的数组

  • 原数组会被改变

var al=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”},

{id:4,name:“999”,score:“99”},

{id:5,name:“888”,score:“89”}

]

console.log(al.copyWithin(1,2,3));

console.log(al)

第一个输出:[ {id:1,name:“My”,score:“90”}, {id:2,name:“枷”,score:“98”}, {id:2,name:“枷”,score:“98”}, {id:4,name:“999”,score:“99”}, {id:5,name:“888”,score:“89”} ]

第二个输出:[ {id:1,name:“My”,score:“90”}, {id:2,name:“枷”,score:“98”}, {id:2,name:“枷”,score:“98”}, {id:4,name:“999”,score:“99”}, {id:5,name:“888”,score:“89”} ]

解释:al.copyWithin(1,2,3),是在数组al上,将数组al[2]-al[3](不包括3)之间的内容复制到1的位置,然依次后往后覆盖

如果:console.log(al.copyWithin(-1));,那么会变成数组的第一个值被复制到了最后一个

Array.prototype.slice(start,end)

-截取数组中开始位置到结束位置的元素(不包括结束位置),浅拷贝生成新的数组

-参数

  • start:开始提取的位置,整数,如果为负数的话,从数组的结束开始往前倒数

  • end:结束提取的位置,可选,不写的话默认到数组结束

-返回值

  • 包含提取元素的新数组

示例:

var am=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

console.log(am.slice(-1)); //输出:[{id:2,name:“枷”,score:“98”}]

console.log(am.slice(0,2)); //输出:[{id:1,name:“My”,score:“90”},{id:3,name:“123”,score:“80”}]

console.log(am.slice(0)); //输出:[{id:1,name:“My”,score:“90”},{id:3,name:“123”,score:“80”},{id:2,name:“枷”,score:“98”}]

另外,注意,因为是浅拷贝,所以slice()得到的是对象的引用,所以对这个引用修改,会导致原数组也会修改,如下例:

am.slice(-1)[0].name=“oop”; //修改里面的name值

console.log(am); //打印出来的发现am[2]里面的name值变了

Array.prototype.concat(…arguments)合并2个或多个数组,参数…arguments是任意多的值,返回值是合并后的数组

var an=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

var ao=[1,2,3]

console.log(an.concat(ao)); //输出:[{id:1,name:“My”,score:“90”},{id:3,name:“123”,score:“80”},{id:2,name:“枷”,score:“98”},1,2,3]

Array.prototype.join(sepatator):将数组中的元素连接到一个字符串里参数sepatator是元素之间的分隔符,可选,返回值是元素拼接的字符串

示例:

var ap=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

]

var aq=[“我”,“是”,“ta”]

console.log(ap.join()); //输出:[object Object],[object Object],[object Object]

console.log(aq.join()); //输出:我,是,ta

console.log(aq.join(“|”)); //输出:我|是|ta

遍历数组

var a=[“,”,‘…’,…,‘asd’]

for(var i=0;i<a.length;i++){

console.log(a[i]);

}

简单方法

Array.pototype.forEach(callbackfn[,thisArg])

遍历数组中的每个元素,对每个元素执行callbackfn函数

-参数

  • callbackfn:数组中每个元素执行的函数,这个函数有三个参数
  1. -currentValue:数组中正在处理的当前元素

  2. -index:数组中正在处理的当前元素的索引

  3. -array:正在操作的数组

  • thisArg:callbackfn函数里this的指向,可选

-返回值,无

var ar=[“我”,“是”,“ta”];

var numfn=function (currentValue,index,array) {

console.log(currentValue,index,array);

}

console.log(ar.forEach(numfn)); //输出:我 0 [“我”, “是”, “ta”] 是 1 [“我”, “是”, “ta”] ta 2 [“我”, “是”, “ta”]

示例2:

var as={

name:[“我”,“是”,“ta”],

isLikeSong:function(value) {

return value===this.name[0]

},

like:function (song) {

if(this.isLikeSong(song)){

console.log(“我喜欢”+song)

}

}

}

console.log(as.name.forEach(as.like,as));

解释:对as.name数组进行遍历,每一次遍历都执行了as.like方法,然后在as.like方法里进行了isLikeSong的判断,判断是否和我们指定的值相等,如果相等,则返回true,并且,如果是true的话,执行了打印,这边目标数组需要填入,否则,默认的是window对象,window对象中没有this.name[0]值,和没有isLikeSong()方法,会报错

Array.prototype.map(callbackfn[,thisArg])

-创建一个新数组,新数组的元素是callbackfn函数的返回结果

-参数

  • callbackfn:生成新数组元素的函数,这个函数有3个参数,分别是
  1. currentValue:数组中正在处理的当前元素

  2. index:数组中正在处理的元素的索引

  3. array:正在操作的数组

  • thisArg:callbackfn函数里this的指向,可选

-返回值:新数组,元素为执行callbackfn函数后的返回值

示例1:

var au=[

{id:1,name:“My”,score:“90”},

{id:3,name:“123”,score:“80”},

{id:2,name:“枷”,score:“98”}

];

var getName=function (item,index,array) {

return item.name;

}

console.log(au.map(getName))

**解释:**获取歌单的歌曲名,通过方法getName每次获取到对象中的name值并返回

var at={

name:[“123”,“223”,“323”],

operationN:function (value) {

return parseInt(this.name[value])+10;

},

fuc:function (num,index) {

console.log(this.operationN(index))

}

}

at.name.map(at.fuc,at)

解释:对对象at的name数组进行+10后返回,通过方法是at.fnc方法,获取当前传入数组的索引,索引值传入at.operationN方法,然后获得name数组对应的值,通过parseInt()方法,将字符串转换成数字+10然后返回

Array.prototype.reduce(callbackfn,[,initialValue])

-使用callbackfn函数从左向右累加处理每个元素,返回累加处理后的

-参数

  • callbackfn:处理每个元素的函数,函数有4个默认参数
  1. accumulator:累加器累加回调的返回值

  2. currentValue:数组中正在处理的当前元素

  3. index:数组中正在处理的当前元素的索引

  4. array:正在操作的数组

  • initialValue:调用callbackfn的第一个输入值,可选

-返回值

  • 函数累加处理后的结果

var av=[

{id:1,name:“My”,score:“80”},

{id:3,name:“123”,score:“90”},

{id:2,name:“枷”,score:“88”}

];

var getScore=function (accumulator,item,index,array) {

return accumulator+parseInt(item.score);

}

console.log(av.reduce(getScore,2)) //输出260,是2+80+90+88

**解释:**通过函数getScore,返回了累加的结果,参数accumulator初始值是2,之后每次+item.scroe的值,因为值是字符串,所以通过parseInt()转换成数字,再累加

Array.prototype.reduceRight(callbackfn,[,initialValue])

-和reduce()相对,使用callbackfn函数从右向左累加处理每个元素,返回累加处理后的值

-参数

  • callbackfn:处理每个元素的函数,函数有4个默认参数
  1. accumulator:累加器累加回调的返回值

  2. currentValue:数组中正在处理的当前元素

  3. index:数组中正在处理的当前元素的索引

  4. array:正在操作的数组

  • initialValue:调用callbackfn的第一个输入值,可选

-返回值

  • 函数累加处理后的结果

var av=[

{id:1,name:“My”,score:“80”},

{id:3,name:“123”,score:“90”},

{id:2,name:“枷”,score:“88”}

];

var getScore=function (accumulator,item,index,array) {

return accumulator+parseInt(item.score);

}

console.log(av.reduceRight(getScore,2)) //输出260,是2+88+90+80

Array.prototype.indexOf(searchElement[,fromIndex])

-在数组中找到给定元素的第一个索引值

-参数

  • searchElement:要查找的元素

  • fromIndex:开始查找的位置,可选,默认是0

-返回值

  • 指定元素在数组中第一次出现的位置,或者-1

var aw=[

{id:1,name:“My”,score:“80”},

{id:3,name:“123”,score:“90”},

{id:2,name:“枷”,score:“88”}

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
e:“123”,score:“90”},

{id:2,name:“枷”,score:“88”}

];

var getScore=function (accumulator,item,index,array) {

return accumulator+parseInt(item.score);

}

console.log(av.reduceRight(getScore,2)) //输出260,是2+88+90+80

Array.prototype.indexOf(searchElement[,fromIndex])

-在数组中找到给定元素的第一个索引值

-参数

  • searchElement:要查找的元素

  • fromIndex:开始查找的位置,可选,默认是0

-返回值

  • 指定元素在数组中第一次出现的位置,或者-1

var aw=[

{id:1,name:“My”,score:“80”},

{id:3,name:“123”,score:“90”},

{id:2,name:“枷”,score:“88”}

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-s8jm6xxg-1714919155424)]

[外链图片转存中…(img-NIf00GmI-1714919155425)]

[外链图片转存中…(img-SsRNBqbD-1714919155425)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值