在我们日常的实际开发中,经常遇到需要各种需要处理的数组,jav儿童睡前故事免费听ascript中虽然提供了各式各样的方法,但本菜鸟很长一段时间都分不清楚这些是干什么用的,也偷懒不去看……
前一段时间在网上冲浪时,看到一个评论里有人用符号表示了一个方法,觉得十分形象生动,于是便花了半天时间重新学习了一些常见的数组方法,并用符号、图标进行具象化的整理,我觉得本菜鸟今天又博学了一点点。
[●,●,■,●].map(●=>■)→[■,■,■,■]
letarr=['杜甫','李白','李商隐','白居易'];letmaparr=arr.map(e=>'苏轼');//console.log(maparr):["苏轼","苏轼","苏轼","苏轼"]
[○,△,□,△].filter(△=>true)→[△,△]
letarr=[{id:0,name:'杜甫',age:'唐'},{id:1,name:'李白',age:'唐'},{id:2,name:'李商隐',age:'唐'},{id:3,name:'苏轼',age:'宋'},{id:4,name:'辛弃疾',age:'宋'}];letfilterarr=arr.filter(e=>e.age==='宋');/***console.log(filterarr):[*{id:3,name:'苏轼',age:'宋'},*{id:4,name:'辛弃疾',age:'宋'}*]*/
[○,△,□,△].find(△=>true)→(first)△
letarr=[{id:0,name:'杜甫',age:'唐'},{id:1,name:'李白',age:'唐'},{id:2,name:'李商隐',age:'唐'},{id:3,name:'苏轼',age:'宋'},{id:4,name:'辛弃疾',age:'宋'}];letfinditem=arr.find(e=>e.age==='宋');/***console.log(finditem):{id:3,name:"苏轼",age:"宋"};*/
[○,△,□,△].findindex(△=>true)→(first)△
letarr=[{id:0,name:'杜甫',age:'唐'},{id:1,name:'李白',age:'唐'},{id:2,name:'李商隐',age:'唐'},{id:3,name:'苏轼',age:'宋'},{id:4,name:'辛弃疾',age:'宋'}];letfinditem=arr.find(e=>e.age==='宋');/***console.log(finditem):{id:3,name:"苏轼",age:"宋"};*/
[○,○,○,△].ev婆婆媳妇和小姑ery(○=>true)→fal
letarr=[{id:0,name:'杜甫',age:'唐'},{id:1,name:'李白',age:'唐'},{id:2,name:'李商隐',age:'唐'},{id:3,name:'苏轼',age:'宋'},{id:4,name:'辛弃疾',age:'宋'}];leteveryflag=arr.every(e=>e.age==='宋');/***console.log(everyflag):fal*/
[△,○,○,△].some(△=>true)→true
letarr=[{id:0,name:'杜甫',age:'唐'},{id:1,name:'李白',age:'唐'},{id:2,name:'李商隐',age:'唐'},{id:3,name:'苏轼',age:'宋'},{id:4,name:'辛弃疾',age:'宋'}];letsomeflag=arr.some(e=>e.age==='宋');/***console.log(someflag):true*/
[○,□,△].concat([△,○])→[○,□,△,△,○]
letarr=[{id:0,name:'杜甫',age:'唐'},{id:1,name:'李白',age:'唐'},{id:2,name:'李商隐',age:'唐'},{id:3,name:'苏轼',age:'宋'},{id:4,name:'辛弃疾',age:'宋'}];letnewarr=[{id:5,name:'李清照',age:'宋'}];letconcatarr=arr.concat(newarr);/*console.log(concatarr):[{id:0,name:'杜甫',age:'唐'},{id:1,name:'李白',age:'唐'},{id:2,name:'李商隐',age:'唐'},{id:3,name:'苏轼',age:'宋'},{id:4,name:'辛弃疾',age:'宋'},{id:5,name:'李清照',age:'宋'}]*/
[○,□,△,○].join('-')→○-□-△-○
letarr=['贝','加','尔','湖','畔'];letjoinstr=arr.join('-')/*console.log(joinstr):贝-加-尔-湖-畔*/
[1,2,3,4].reduce((total,current)=>total+current,10)→20
letarr=[1,2,3,4];letreduceres=arr.reduce((total,current)=>total+current,10);/*console.log(reduceres):20*/
[●,●,■,●].foreach(●=>■)→[■,■,■,■]letarr=[{id:0,name:'杜甫'},{id:1,name:'李白'},{id:2,name:'李商隐'}]letforeacharr=arr.foreach(e=>e.age='唐')/***arr:[*{id:0,name:'杜甫',age:'唐'},*{id:1,name:'李白',age:'唐'},*{id:2,name:'李商隐',age:'唐'}*]**foreacharr:undefined*/
[○,□,[△,[△,○]]].fill(infinity)→[○,□,△,△,○]
letarr=[1,2,[[3],4]];arr.flat(infinity);/*console.log(arr):[1,ltaly2,3,4]*/
[○,□,△,○].fill(,2,3)→[○,□,,○]
letarr=[1,2,3,4];arr.fill('你好',2,3);/*console.log(arr):[1,2,'你好',4]*/
本文发布于:2023-04-05 09:53:09,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/3c45f754cf0ea5632500756738992aee.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:js的数组有哪些常用方法(超详细的 JS 数组方法).doc
本文 PDF 下载地址:js的数组有哪些常用方法(超详细的 JS 数组方法).pdf
留言与评论(共有 0 条评论) |