cainiaoblog 23.2.171
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CNAME +1 -0
- package/MP_verify_o6dHCK5mIkxNpzvZ.txt +1 -0
- package/README.md +8 -0
- package/about/index.html +54 -0
- package/ads.txt +1 -0
- package/app/manual.html +104 -0
- package/appConfigs/config +1 -0
- package/archives/2018/index.html +48 -0
- package/archives/2019/index.html +48 -0
- package/archives/2019/page/2/index.html +48 -0
- package/archives/2020/index.html +48 -0
- package/archives/2021/index.html +48 -0
- package/archives/2022/index.html +48 -0
- package/archives/2023/index.html +48 -0
- package/archives/index.html +48 -0
- package/archives/page/2/index.html +48 -0
- package/archives/page/3/index.html +48 -0
- package/article/10047.html +148 -0
- package/article/10164.html +64 -0
- package/article/10624.html +436 -0
- package/article/10745.html +586 -0
- package/article/11674.html +71 -0
- package/article/11939.html +72 -0
- package/article/12238.html +86 -0
- package/article/1230.html +533 -0
- package/article/13265.html +69 -0
- package/article/13823.html +78 -0
- package/article/14125.html +72 -0
- package/article/14192.html +561 -0
- package/article/14375.html +85 -0
- package/article/14744.html +83 -0
- package/article/14936.html +451 -0
- package/article/1518.html +250 -0
- package/article/15428.html +71 -0
- package/article/15518.html +288 -0
- package/article/15569.html +418 -0
- package/article/1566.html +122 -0
- package/article/16017.html +156 -0
- package/article/16947.html +440 -0
- package/article/17096.html +82 -0
- package/article/17246.html +89 -0
- package/article/17575.html +350 -0
- package/article/179.html +757 -0
- package/article/18039.html +233 -0
- package/article/18071.html +375 -0
- package/article/18142.html +71 -0
- package/article/19909.html +160 -0
- package/article/20014.html +89 -0
- package/article/20021.html +389 -0
- package/article/20037.html +280 -0
- package/article/20056.html +95 -0
- package/article/20093.html +404 -0
- package/article/2021.html +607 -0
- package/article/20459.html +115 -0
- package/article/20613.html +263 -0
- package/article/20635.html +71 -0
- package/article/21317.html +71 -0
- package/article/21320.html +90 -0
- package/article/21782.html +548 -0
- package/article/22230.html +435 -0
- package/article/22237.html +347 -0
- package/article/22398.html +109 -0
- package/article/22422.html +270 -0
- package/article/22565.html +300 -0
- package/article/23046.html +250 -0
- package/article/23083.html +215 -0
- package/article/23195.html +297 -0
- package/article/23301.html +835 -0
- package/article/23589.html +285 -0
- package/article/23655.html +338 -0
- package/article/23864.html +76 -0
- package/article/23914.html +96 -0
- package/article/24169.html +135 -0
- package/article/24581.html +349 -0
- package/article/2475.html +500 -0
- package/article/24826.html +77 -0
- package/article/24924.html +247 -0
- package/article/25164.html +1495 -0
- package/article/25318.html +394 -0
- package/article/25710.html +1129 -0
- package/article/25734.html +83 -0
- package/article/26083.html +174 -0
- package/article/26147.html +283 -0
- package/article/26291.html +110 -0
- package/article/26354.html +71 -0
- package/article/26624.html +145 -0
- package/article/26639.html +298 -0
- package/article/26850.html +138 -0
- package/article/26940.html +258 -0
- package/article/27796.html +190 -0
- package/article/27800.html +161 -0
- package/article/28963.html +159 -0
- package/article/28976.html +247 -0
- package/article/29042.html +72 -0
- package/article/29596.html +81 -0
- package/article/29619.html +183 -0
- package/article/29623.html +329 -0
- package/article/29829.html +205 -0
- package/article/29abbd1c.html +112 -0
- package/article/30215.html +178 -0
- package/article/30552.html +78 -0
- package/article/30818.html +201 -0
- package/article/31330.html +364 -0
- package/article/3154.html +577 -0
- package/article/31695.html +348 -0
- package/article/31826.html +71 -0
- package/article/31843.html +132 -0
- package/article/32280.html +212 -0
- package/article/32431.html +74 -0
- package/article/32443.html +72 -0
- package/article/32732.html +122 -0
- package/article/3286.html +251 -0
- package/article/32960.html +198 -0
- package/article/33713.html +72 -0
- package/article/33899.html +615 -0
- package/article/34351.html +74 -0
- package/article/34372.html +89 -0
- package/article/34410.html +74 -0
- package/article/34431.html +118 -0
- package/article/3447.html +75 -0
- package/article/34474.html +142 -0
- package/article/34871.html +147 -0
- package/article/35397.html +175 -0
- package/article/3544.html +199 -0
- package/article/35503.html +115 -0
- package/article/35624.html +84 -0
- package/article/35697.html +384 -0
- package/article/35863.html +73 -0
- package/article/36037.html +253 -0
- package/article/36118.html +71 -0
- package/article/3621.html +119 -0
- package/article/36251.html +296 -0
- package/article/36642.html +352 -0
- package/article/37102.html +763 -0
- package/article/37336.html +351 -0
- package/article/37351.html +89 -0
- package/article/3760.html +294 -0
- package/article/37749.html +75 -0
- package/article/37789.html +83 -0
- package/article/38005.html +765 -0
- package/article/38009.html +284 -0
- package/article/38269.html +194 -0
- package/article/38344.html +88 -0
- package/article/38359.html +334 -0
- package/article/38423.html +379 -0
- package/article/38530.html +213 -0
- package/article/38743.html +268 -0
- package/article/38753.html +204 -0
- package/article/38881.html +71 -0
- package/article/39087.html +414 -0
- package/article/39309.html +84 -0
- package/article/39486.html +74 -0
- package/article/39637.html +306 -0
- package/article/39839.html +72 -0
- package/article/39937.html +152 -0
- package/article/39970.html +71 -0
- package/article/402.html +559 -0
- package/article/40698.html +71 -0
- package/article/40811.html +71 -0
- package/article/40964.html +462 -0
- package/article/41011.html +322 -0
- package/article/41152.html +78 -0
- package/article/41696.html +71 -0
- package/article/41717.html +281 -0
- package/article/41777.html +149 -0
- package/article/419.html +985 -0
- package/article/4233.html +136 -0
- package/article/4292.html +86 -0
- package/article/43302.html +96 -0
- package/article/43397.html +106 -0
- package/article/43724.html +281 -0
- package/article/43999.html +224 -0
- package/article/44426.html +77 -0
- package/article/44964.html +137 -0
- package/article/45004.html +123 -0
- package/article/4510.html +83 -0
- package/article/45156.html +320 -0
- package/article/45157.html +52 -0
- package/article/45267.html +87 -0
- package/article/45641.html +207 -0
- package/article/45960.html +75 -0
- package/article/46100.html +1262 -0
- package/article/46366.html +74 -0
- package/article/46771.html +96 -0
- package/article/46911.html +517 -0
- package/article/46948.html +137 -0
- package/article/46ee4850.html +349 -0
- package/article/47019.html +84 -0
- package/article/47532.html +94 -0
- package/article/4783.html +429 -0
- package/article/47972.html +72 -0
- package/article/48216.html +127 -0
- package/article/48252.html +185 -0
- package/article/48520.html +74 -0
- package/article/48722.html +75 -0
- package/article/48807.html +99 -0
- package/article/49126.html +323 -0
- package/article/50001.html +88 -0
- package/article/50058.html +151 -0
- package/article/50343.html +410 -0
- package/article/50510.html +82 -0
- package/article/51082.html +98 -0
- package/article/51677.html +111 -0
- package/article/5204.html +1085 -0
- package/article/52500.html +538 -0
- package/article/52689.html +259 -0
- package/article/52877.html +569 -0
- package/article/53125.html +191 -0
- package/article/53428.html +251 -0
- package/article/53524.html +74 -0
- package/article/53698.html +322 -0
- package/article/53803e6.html +93 -0
- package/article/53924.html +291 -0
- package/article/53962.html +253 -0
- package/article/54006.html +71 -0
- package/article/54018.html +88 -0
- package/article/54161.html +83 -0
- package/article/54178.html +69 -0
- package/article/54258.html +327 -0
- package/article/54295.html +214 -0
- package/article/54313.html +1976 -0
- package/article/54351.html +90 -0
- package/article/54604.html +80 -0
- package/article/54632.html +71 -0
- package/article/54912.html +756 -0
- package/article/54918.html +129 -0
- package/article/55119.html +136 -0
- package/article/55756.html +118 -0
- package/article/55769.html +136 -0
- package/article/55870.html +161 -0
- package/article/56139.html +92 -0
- package/article/56557.html +69 -0
- package/article/56604.html +69 -0
- package/article/56816.html +266 -0
- package/article/56926.html +153 -0
- package/article/57747.html +409 -0
- package/article/57965.html +475 -0
- package/article/58816.html +78 -0
- package/article/59059.html +435 -0
- package/article/59095.html +350 -0
- package/article/59349.html +244 -0
- package/article/59402.html +216 -0
- package/article/59448.html +55 -0
- package/article/59870.html +94 -0
- package/article/59877.html +208 -0
- package/article/5dbb6f41.html +86 -0
- package/article/6008.html +75 -0
- package/article/60327.html +126 -0
- package/article/6035.html +76 -0
- package/article/60428.html +71 -0
- package/article/60544.html +128 -0
- package/article/61319.html +95 -0
- package/article/61367.html +93 -0
- package/article/624d1918.html +292 -0
- package/article/62543.html +71 -0
- package/article/62887.html +169 -0
- package/article/63074.html +120 -0
- package/article/63266.html +311 -0
- package/article/63351.html +74 -0
- package/article/63542.html +104 -0
- package/article/63824.html +275 -0
- package/article/64051.html +653 -0
- package/article/64189b69.html +72 -0
- package/article/64354.html +75 -0
- package/article/64869.html +79 -0
- package/article/64923.html +596 -0
- package/article/65128.html +95 -0
- package/article/65295.html +79 -0
- package/article/6888.html +75 -0
- package/article/7065.html +84 -0
- package/article/7455.html +85 -0
- package/article/7a3ef8f.html +315 -0
- package/article/8140.html +188 -0
- package/article/8190.html +85 -0
- package/article/8225.html +78 -0
- package/article/84c9ccd6.html +159 -0
- package/article/8f9a9ae3.html +82 -0
- package/article/9174.html +73 -0
- package/article/9738.html +294 -0
- package/article/a4145266.html +71 -0
- package/article/bedea419.html +72 -0
- package/article/bf7e7421.html +78 -0
- package/article/c1867fbf.html +103 -0
- package/article/c5576490.html +70 -0
- package/article/dcb0db28.html +76 -0
- package/article/fc3b727a.html +79 -0
- package/atom.xml +445 -0
- package/baidusitemap.xml +1079 -0
- package/blocks/dns.txt +264 -0
- package/blocks/filter.txt +1070 -0
- package/blocks/script.js +42 -0
- package/categories/CDN/index.html +48 -0
- package/categories/IDE/index.html +48 -0
- package/categories/git/index.html +48 -0
- package/categories/hexo/index.html +48 -0
- package/categories//345/205/254/345/205/261/350/212/202/346/227/245/index.html +48 -0
- package/categories//345/210/206/344/272/253/index.html +48 -0
- package/categories//345/211/215/347/253/257/index.html +48 -0
- package/categories//345/211/215/347/253/257/page/2/index.html +48 -0
- package/categories//345/211/215/347/253/257//346/265/217/350/247/210/345/231/250/index.html +48 -0
- package/categories//345/211/215/347/253/257//346/265/217/350/247/210/345/231/250//347/275/221/347/273/234/345/215/217/350/256/256/index.html +48 -0
- package/categories//345/215/232/345/256/242/index.html +48 -0
- package/categories//345/267/245/345/205/267/index.html +48 -0
- package/categories//345/271/277/345/221/212/350/277/207/346/273/244/index.html +48 -0
- package/categories//345/271/277/345/221/212/350/277/207/346/273/244//350/247/206/351/242/221/347/224/265/350/247/206/345/260/217/350/257/264/346/274/253/347/224/273/350/265/204/350/256/257/351/237/263/344/271/220/index.html +48 -0
- package/categories//346/255/243/345/210/231/index.html +48 -0
- package/categories//346/265/217/350/247/210/345/231/250/index.html +48 -0
- package/categories//347/256/227/346/263/225/index.html +48 -0
- package/categories//350/265/204/350/256/257/index.html +48 -0
- package/categories//350/275/273/346/235/276/344/270/200/345/210/273/index.html +48 -0
- package/cdn/css/font-awesome.min.css +4 -0
- package/cdn/css/grids-responsive-min.css +7 -0
- package/cdn/css/normalize.css +349 -0
- package/cdn/css/pure-min.css +11 -0
- package/cdn/fonts/fontawesome-webfont.eot +0 -0
- package/cdn/fonts/fontawesome-webfont.svg +2671 -0
- package/cdn/fonts/fontawesome-webfont.ttf +0 -0
- package/cdn/fonts/fontawesome-webfont.woff +0 -0
- package/cdn/fonts/fontawesome-webfont.woff2 +0 -0
- package/cdn/js/Valine.min.js +17 -0
- package/cdn/js/canvas-nest.js +1 -0
- package/cdn/js/clipboard.min.js +7 -0
- package/cdn/js/crypto-js.js +25 -0
- package/cdn/js/jquery.min.js +2 -0
- package/css/default.css +1144 -0
- package/css/donate.css +338 -0
- package/css/hbe.style.css +749 -0
- package/css/style.css +2095 -0
- package/donate/index.html +39 -0
- package/google917dcf72f6e5c7f5.html +1 -0
- package/guestbook/index.html +68 -0
- package/hosts/ads.txt +149 -0
- package/hosts/flash.txt +9 -0
- package/hosts/github.txt +6 -0
- package/hosts/google.txt +16936 -0
- package/hosts/winrar.txt +2 -0
- package/img/alipay.svg +46 -0
- package/img/bitcoin.svg +135 -0
- package/img/blog.ico +0 -0
- package/img/blog.png +0 -0
- package/img/gaba.png +0 -0
- package/img/github.svg +1 -0
- package/img/like.svg +1 -0
- package/img/mp-mini.jpg +0 -0
- package/img/mp.png +0 -0
- package/img/paypal.svg +63 -0
- package/img/upy_logo.svg +59 -0
- package/img/wechat.svg +49 -0
- package/index.html +48 -0
- package/jd_root.txt +1 -0
- package/js/codeblock-resizer.js +51 -0
- package/js/dark.js +12 -0
- package/js/donate.js +87 -0
- package/js/fixedPage.js +106 -0
- package/js/gitment.browser.js +3751 -0
- package/js/search.js +86 -0
- package/js/share.js +60 -0
- package/js/smartresize.js +32 -0
- package/js/totop.js +12 -0
- package/kankan/index.html +77 -0
- package/lib/hbe.js +293 -0
- package/package.json +25 -0
- package/page/10/index.html +49 -0
- package/page/11/index.html +59 -0
- package/page/12/index.html +67 -0
- package/page/13/index.html +63 -0
- package/page/14/index.html +66 -0
- package/page/15/index.html +60 -0
- package/page/16/index.html +60 -0
- package/page/17/index.html +62 -0
- package/page/18/index.html +61 -0
- package/page/19/index.html +58 -0
- package/page/2/index.html +48 -0
- package/page/20/index.html +57 -0
- package/page/21/index.html +61 -0
- package/page/22/index.html +55 -0
- package/page/23/index.html +64 -0
- package/page/24/index.html +60 -0
- package/page/25/index.html +58 -0
- package/page/26/index.html +48 -0
- package/page/27/index.html +58 -0
- package/page/3/index.html +48 -0
- package/page/4/index.html +48 -0
- package/page/5/index.html +51 -0
- package/page/6/index.html +51 -0
- package/page/7/index.html +51 -0
- package/page/8/index.html +53 -0
- package/page/9/index.html +48 -0
- package/randomcall/README.html +1 -0
- package/randomcall/lucker.html +290 -0
- package/randomcall/lucky.png +0 -0
- package/robots.txt +22 -0
- package/search.xml +6740 -0
- package/sitemap.xml +2669 -0
- package/tags//345/216/237/345/210/233/index.html +48 -0
- package/tags//345/250/261/344/271/220/index.html +48 -0
- package/tags//346/211/213/345/206/231/index.html +48 -0
- package/tags//347/226/253/346/203/205/index.html +48 -0
- package/tags//350/275/254/350/275/275/index.html +48 -0
- package/tags//350/275/254/350/275/275/page/2/index.html +48 -0
- package/v.html +90 -0
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
<!DOCTYPE html><html lang="zh-CN" data-dark><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"><meta content="yes" name="apple-mobile-web-app-capable"><meta content="black-translucent" name="apple-mobile-web-app-status-bar-style"><meta content="telephone=no" name="format-detection"><meta name="description" content="前端壹菜鸟,记录工作难题,关注前端知识,收集精彩博文,做技术的搬运工"><meta name="msvalidate.01" content="0FE4D8B3381D3D87088996B886E1E2BD"><title>JavaScript中数组的15种常见操作(备忘单) | 前端壹菜鸟</title><link rel="stylesheet" type="text/css" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/css/style.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/normalize.css@latest/normalize.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/purecss@latest/build/pure-min.min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/purecss@latest/build/grids-responsive-min.css"><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"><link rel="icon" mask="" sizes="any" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.ico"><link rel="Shortcut Icon" type="image/x-icon" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.ico"><link rel="apple-touch-icon" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.png"><link rel="apple-touch-icon-precomposed" href="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/blog.png"><link rel="alternate" type="application/atom+xml" href="/atom.xml"><script type="text/javascript" src="//cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8385136408348258"></script><script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script><script>(function(){
|
|
2
|
+
var bp = document.createElement('script');
|
|
3
|
+
var curProtocol = window.location.protocol.split(':')[0];
|
|
4
|
+
if (curProtocol === 'https'){
|
|
5
|
+
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
|
|
6
|
+
}
|
|
7
|
+
else{
|
|
8
|
+
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
|
|
9
|
+
}
|
|
10
|
+
var s = document.getElementsByTagName("script")[0];
|
|
11
|
+
s.parentNode.insertBefore(bp, s);
|
|
12
|
+
})();
|
|
13
|
+
</script><script>(function(){
|
|
14
|
+
var src = 'https://jspassport.ssl.qhimg.com/11.0.1.js?d182b3f28525f2db83acfaaf6e696dba';
|
|
15
|
+
document.write('<script src="' + src + '" id="sozz"><\/script>');
|
|
16
|
+
})();
|
|
17
|
+
</script><script>var _hmt=_hmt||[];(function(){var hm=document.createElement("script");hm.src='https://hm.baidu.com/hm.js?011f0b44e8452bfa57fbfa23c5fe7683';var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(hm, s);})();
|
|
18
|
+
</script><script async src="https://www.googletagmanager.com/gtag/js?id=UA-150860401-1"></script><script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);};gtag('js',new Date());gtag('config','UA-150860401-1');
|
|
19
|
+
</script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/fixedPage.min.js"></script><meta name="generator" content="Hexo 6.3.0"></head><body><div class="body_container"><div id="header"><div class="site-name"><a id="logo" href="/.">前端壹菜鸟</a><p class="description">关注前端知识,收集精彩博文,做技术的搬运工</p></div><div id="nav-menu"><a class="current" href="/." target="_self"><i class="fa fa-home"> 首页</i></a><a href="/archives/" target="_self"><i class="fa fa-archive"> 归档</i></a><a href="/guestbook/" target="_self"><i class="fa fa-comments"> 留言</i></a><a href="/about/" target="_self"><i class="fa fa-user"> 关于</i></a><a href="https://laonongmin.online/" target="_target"><i class="fa fa-group"> 看看</i></a><a href="https://love.xuehuayu.cn/" target="_target"><i class="fa fa-heart"> LOVE</i></a></div></div><div class="pure-g" id="layout"><div class="pure-u-1 pure-u-md-3-4"><div class="content_container"><div class="post"><h1 class="post-title c-post">JavaScript中数组的15种常见操作(备忘单)</h1><div class="post-meta"><span class="date">2019-12-09</span><span> | </span><span class="tag"><a href="/tags/%E8%BD%AC%E8%BD%BD/">转载 </a></span><span> | </span><span class="category"><a href="/categories/%E5%89%8D%E7%AB%AF/">前端 </a></span><span id="busuanzi_container_page_pv"> | <span id="busuanzi_value_page_pv"><i class="fa fa-spinner"></i></span><span> 阅读</span></span><span class="post-time"><span class="post-meta-item-text"> | </span><span class="post-meta-item-icon"><i class="fa fa-keyboard-o"></i><span class="post-count"> 3.5k</span><span class="post-meta-item-text"> 字</span></span></span><span class="post-time"> | <span class="post-meta-item-icon"><i class="fa fa-hourglass-half"></i><span class="post-count"> 15</span><span class="post-meta-item-text"> 分钟</span></span></span></div><a class="disqus-comment-count" href="/article/1230.html#vcomment"><span class="waline-comment-count" data-path="/article/1230.html"></span><span> 条评论</span></a><div class="post-content"><p><code>原文地址:https://www.html.cn/web/javascript/14284.html</code></p>
|
|
20
|
+
<p>数组是 JS 中广泛使用的数据结构。数组对象提供了大量有用的方法,如<code>array. forEach()</code>、<code>array.map()</code>等来操作数组。</p>
|
|
21
|
+
<span id="more"></span>
|
|
22
|
+
|
|
23
|
+
<p><img src="https://img.php.cn/upload/article/000/000/006/5ddf2597f3918924.jpg" alt="xuehuayu.cn"></p>
|
|
24
|
+
<p>在实战中,我经常对数组可能的操作和相应采用哪个更好的方法不知所措,所以本文就列出 <code>15</code> 种常用数据方法,让咱们重温加强记忆一下。</p>
|
|
25
|
+
<h2 id="1-数组的遍历"><a href="#1-数组的遍历" class="headerlink" title="1. 数组的遍历"></a>1. 数组的遍历</h2><p><strong>1.1 <code>for..of</code> 循环</strong></p>
|
|
26
|
+
<p><code>for(const item of items)</code>循环遍历数组项,如下所示遍历<code>colors</code>列表:</p>
|
|
27
|
+
<pre><code>const colors = ['blue', 'green', 'white'];
|
|
28
|
+
|
|
29
|
+
for (const color of colors) {
|
|
30
|
+
console.log(color);
|
|
31
|
+
}
|
|
32
|
+
// 'blue'
|
|
33
|
+
// 'green'
|
|
34
|
+
// 'white'
|
|
35
|
+
</code></pre>
|
|
36
|
+
<p><strong>提示:</strong></p>
|
|
37
|
+
<p>咱们可以随时使用<code>break</code>语句停止遍历。</p>
|
|
38
|
+
<p><strong>1.2 <code>for</code> 循环</strong></p>
|
|
39
|
+
<p><code>for(let i; i < array.length; i++)</code>循环使用递增的索引变量遍历数组项。</p>
|
|
40
|
+
<p><code>for</code>通常需要在每个循环中递增<code>index</code> 变量</p>
|
|
41
|
+
<pre><code>const colors = ['blue', 'green', 'white'];
|
|
42
|
+
|
|
43
|
+
for (let index = 0; index < colors.length; index++) {
|
|
44
|
+
const color = colors[index];
|
|
45
|
+
console.log(color);
|
|
46
|
+
}
|
|
47
|
+
// 'blue'
|
|
48
|
+
// 'green'
|
|
49
|
+
// 'white'
|
|
50
|
+
</code></pre>
|
|
51
|
+
<p><code>index</code>变量从<code>0</code>递增到<code>colors.length-1</code>。此变量用于按以下索引访问项:<code>colors [index]</code>。</p>
|
|
52
|
+
<p><strong>提示</strong></p>
|
|
53
|
+
<p>咱们可以随时使用<code>break</code>语句停止遍历。</p>
|
|
54
|
+
<p><strong>1.3 <code>array.forEach()</code> 方法</strong></p>
|
|
55
|
+
<p><code>array.forEach(callback)</code>方法通过在每个数组项上调用<code>callback</code>函数来遍历数组项。</p>
|
|
56
|
+
<p>在每次遍历中,都使用以下参数调用<code>callback(item [, index [, array]])</code>:当前遍历项,当前遍历索引和数组本身。</p>
|
|
57
|
+
<pre><code>const colors = ['blue', 'green', 'white'];
|
|
58
|
+
|
|
59
|
+
colors.forEach(function callback(value, index) {
|
|
60
|
+
console.log(value, index);
|
|
61
|
+
});
|
|
62
|
+
// 'blue', 0
|
|
63
|
+
// 'green', 1
|
|
64
|
+
// 'white', 2
|
|
65
|
+
</code></pre>
|
|
66
|
+
<p><strong>提示:</strong></p>
|
|
67
|
+
<p>咱们不能中断<code>array.forEach()</code>迭代。</p>
|
|
68
|
+
<h2 id="2-数组的映射"><a href="#2-数组的映射" class="headerlink" title="2. 数组的映射"></a>2. 数组的映射</h2><p>**2.1 ****<code>Array.map()</code>方法**</p>
|
|
69
|
+
<p><code>array.map(callback)</code> 方法通过在每个数组项上使用<code>callback</code>调用结果来创建一个新数组。</p>
|
|
70
|
+
<p>在每个遍历中的<code>callback(item[, index[, array]])</code>使用参数调用:当前项、索引和数组本身,并应该返回新项。</p>
|
|
71
|
+
<p>如下所示咱们对每个数组元素都递增 <code>1</code>:</p>
|
|
72
|
+
<pre><code>const numbers = [0, 2, 4];
|
|
73
|
+
|
|
74
|
+
const newNumbers = numbers.map(function increment(number) {
|
|
75
|
+
return number + 1;
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
newNumbers; // => [1, 3, 5]
|
|
79
|
+
</code></pre>
|
|
80
|
+
<p><strong>提示:</strong></p>
|
|
81
|
+
<p><code>array.map()</code>创建一个新的映射数组,而不改变原始数组。</p>
|
|
82
|
+
<p>**2.2 ****<code>Array.from()</code>方法**</p>
|
|
83
|
+
<p><code>Array.from(arrayLike[, callback])</code>方法通过在每个数组项上使用<code>callback</code> 调用结果来创建一个新数组。</p>
|
|
84
|
+
<p>在每个遍历中<code>callback(item[, index[, array]])</code>使用参数调用:当前项、索引和数组本身并且应该返回新项。</p>
|
|
85
|
+
<p>如下所示咱们对每个数组元素都递增 <code>1</code>:</p>
|
|
86
|
+
<pre><code>const numbers = [0, 2, 4];
|
|
87
|
+
|
|
88
|
+
const newNumbers = Array.from(numbers,
|
|
89
|
+
function increment(number) {
|
|
90
|
+
return number + 1;
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
newNumbers; // => [1, 3, 5]
|
|
95
|
+
</code></pre>
|
|
96
|
+
<p><strong>提示:</strong></p>
|
|
97
|
+
<ul>
|
|
98
|
+
<li><code>Array.from()</code>创建一个新的映射数组,而不改变原始数组。</li>
|
|
99
|
+
<li><code>Array.from()</code>更适合从类似数组的对象进行映射。</li>
|
|
100
|
+
</ul>
|
|
101
|
+
<h2 id="3-数据的简化"><a href="#3-数据的简化" class="headerlink" title="3. 数据的简化"></a>3. 数据的简化</h2><p>**3.1 ****<code>Array.reduce()</code> 方法**</p>
|
|
102
|
+
<p><code>array.reduce(callback[, initialValue])</code>通过调用<code>callback</code> 函数来将数组简化为一个值。</p>
|
|
103
|
+
<p>在每次遍历中的<code>callback(accumulator, item[, index[, array]])</code>使用用参数调用的:累加器,当前项,索引和数组本身且应该返回累加器。</p>
|
|
104
|
+
<p>经典示例是对数字数组求和:</p>
|
|
105
|
+
<pre><code>const numbers = [2, 0, 4];
|
|
106
|
+
|
|
107
|
+
function summarize(accumulator, number) {
|
|
108
|
+
return accumulator + number;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const sum = numbers.reduce(summarize, 0);
|
|
112
|
+
|
|
113
|
+
sum; // => 6
|
|
114
|
+
</code></pre>
|
|
115
|
+
<p>第一步,将<code>accumulator</code> 初始化为<code>0</code>。然后,对每个累加数字和的数组项调用<code>summary</code>函数。</p>
|
|
116
|
+
<p><strong>提示:</strong></p>
|
|
117
|
+
<p>如果没有使用 initialValue 来设置初始值,则默认使用数组的第一个元素作为初始值。</p>
|
|
118
|
+
<h2 id="4-数据的连接"><a href="#4-数据的连接" class="headerlink" title="4. 数据的连接"></a>4. 数据的连接</h2><p>**4.1 ****<code>array.concat()</code> 方法**</p>
|
|
119
|
+
<p><code>array.concat(array1[, array2, ...])</code>将一个或多个数组连接到原始数组。如下所示,连接两个数组:</p>
|
|
120
|
+
<pre><code>const heroes = ['Batman', 'Robin'];
|
|
121
|
+
const villains = ['Joker', 'Bane'];
|
|
122
|
+
|
|
123
|
+
const everyone = heroes.concat(villains);
|
|
124
|
+
|
|
125
|
+
everyone; // => ['Batman', 'Robin', 'Joker', 'Bane']
|
|
126
|
+
</code></pre>
|
|
127
|
+
<p><strong>提示:</strong></p>
|
|
128
|
+
<ul>
|
|
129
|
+
<li><code>concat()</code>创建一个新的数组,而不改变原来的数组</li>
|
|
130
|
+
<li><code>array.concat(array1 [,array2,...])</code> 接受多个要连接的数组。</li>
|
|
131
|
+
</ul>
|
|
132
|
+
<p><strong>4.2 展开操作符号</strong></p>
|
|
133
|
+
<p>咱们将展开操作符与数组字面量一起使用来连接数组:<code>[...array1, ...array2]</code>。</p>
|
|
134
|
+
<pre><code>const heroes = ['Batman', 'Catwoman'];
|
|
135
|
+
const villains = ['Joker', 'Bane'];
|
|
136
|
+
|
|
137
|
+
const names = [...heroes, ...villains];
|
|
138
|
+
|
|
139
|
+
names; // => ['Batman', 'Catwoman', 'Joker', 'Bane']
|
|
140
|
+
</code></pre>
|
|
141
|
+
<p><strong>提示:</strong></p>
|
|
142
|
+
<p><code>[...arr1, ...arr2, ...arrN]</code>:咱们可以使用展开运算符连接所需数量的数组。</p>
|
|
143
|
+
<h2 id="获取数组的片段"><a href="#获取数组的片段" class="headerlink" title="获取数组的片段"></a>获取数组的片段</h2><p>**5.1 ****<code>array.slice()</code> 方法**</p>
|
|
144
|
+
<p><code>array.slice([fromIndex [,toIndex]])</code>返回数组的一个片段,该片段从<code>fromIndex</code>开始,以<code>toIndex</code>结尾(不包括<code>toIndex</code>本身)。<code>fromIndex</code>可选参数默认为<code>0</code>,<code>toIndex</code>可选参数默认为<code>array.length</code>。</p>
|
|
145
|
+
<pre><code>const names = ['Batman', 'Catwoman', 'Joker', 'Bane'];
|
|
146
|
+
|
|
147
|
+
const heroes = names.slice(0, 2);
|
|
148
|
+
const villains = names.slice(2);
|
|
149
|
+
|
|
150
|
+
heroes; // => ['Batman', 'Catwoman']
|
|
151
|
+
villains; // => ['Joker', 'Bane']
|
|
152
|
+
</code></pre>
|
|
153
|
+
<p><strong>提示:</strong></p>
|
|
154
|
+
<p><code>array.slice()</code> 创建一个新数组,而不改变原始数组。</p>
|
|
155
|
+
<h2 id="6-数组的拷贝"><a href="#6-数组的拷贝" class="headerlink" title="6. 数组的拷贝"></a>6. 数组的拷贝</h2><p><strong>6.1 展开操作符</strong></p>
|
|
156
|
+
<p>拷贝数组的一种简单方法是使用展开运算符:<code>const clone = [... array]</code>,如下所示,拷贝 <code>colors</code> 数组:</p>
|
|
157
|
+
<pre><code>const colors = ['white', 'black', 'gray'];
|
|
158
|
+
|
|
159
|
+
const clone = [...colors];
|
|
160
|
+
|
|
161
|
+
clone; // => ['white', 'black', 'gray']
|
|
162
|
+
colors === clone; // => false
|
|
163
|
+
</code></pre>
|
|
164
|
+
<p>提示:</p>
|
|
165
|
+
<p><code>[...array]</code> 创建一个浅拷贝。</p>
|
|
166
|
+
<p>**6.2 ****<code>array.concat()</code>方法**</p>
|
|
167
|
+
<p><code>[].concat(array)</code>是另一种拷贝数组的方法。</p>
|
|
168
|
+
<pre><code>const colors = ['white', 'black', 'gray'];
|
|
169
|
+
|
|
170
|
+
const clone = [].concat(colors);
|
|
171
|
+
|
|
172
|
+
clone; // => ['white', 'black', 'gray']
|
|
173
|
+
colors === clone; // => false
|
|
174
|
+
</code></pre>
|
|
175
|
+
<p>提示:</p>
|
|
176
|
+
<p><code>[].concat(array)</code> 创建一个浅拷贝。</p>
|
|
177
|
+
<p>**6.3 ****<code>array.slice()</code> 方法**</p>
|
|
178
|
+
<p><code>array.slice())</code>是另一种拷贝数组的方法。</p>
|
|
179
|
+
<pre><code>const colors = ['white', 'black', 'gray'];
|
|
180
|
+
|
|
181
|
+
const clone = colors.slice();
|
|
182
|
+
|
|
183
|
+
clone; // => ['white', 'black', 'gray']
|
|
184
|
+
colors === clone; // => false
|
|
185
|
+
</code></pre>
|
|
186
|
+
<p>提示:</p>
|
|
187
|
+
<p><code>colors.slice()</code> 创建一个浅拷贝。</p>
|
|
188
|
+
<h2 id="7-查找数组"><a href="#7-查找数组" class="headerlink" title="7. 查找数组"></a>7. 查找数组</h2><p>**7.1 ****<code>array.includes()</code> 方法**</p>
|
|
189
|
+
<p><code>array.includes(itemToSearch [,fromIndex])</code>返回一个布尔值,<code>array</code> 是否包含<code>itemToSearch</code>。 可选参数<code>fromIndex</code>,默认为<code>0</code>,表示开始搜索的索引。如下所示:判断<code>2</code>和<code>99</code>是否存在于一组数字中:</p>
|
|
190
|
+
<pre><code>const numbers = [1, 2, 3, 4, 5];
|
|
191
|
+
|
|
192
|
+
numbers.includes(2); // => true
|
|
193
|
+
numbers.includes(99); // => false
|
|
194
|
+
</code></pre>
|
|
195
|
+
<p>**7.2 ****<code>array.find()</code> 方法**</p>
|
|
196
|
+
<p><code>array.find(predicate)</code> 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 <code>undefined</code>。</p>
|
|
197
|
+
<p>如下所示,找到数组中的第一个偶数:</p>
|
|
198
|
+
<pre><code>const numbers = [1, 2, 3, 4, 5];
|
|
199
|
+
|
|
200
|
+
function isEven(number) {
|
|
201
|
+
return number % 2 === 0;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const evenNumber = numbers.find(isEven);
|
|
205
|
+
|
|
206
|
+
evenNumber; // => 2
|
|
207
|
+
</code></pre>
|
|
208
|
+
<p>**7.3 ****<code>array.indexOf()</code> 方法**</p>
|
|
209
|
+
<p><code>array.indexOf(itemToSearch[, fromIndex])</code> 返回<code>array</code>中第一个出现的<code>itemToSearch</code>的索引。默认为<code>0的</code>可选参数<code>fromIndex</code>表示开始搜索的索引。</p>
|
|
210
|
+
<p>如下所示,找到<code>Catwoman</code>的索引:</p>
|
|
211
|
+
<pre><code>const names = ['Batman', 'Catwoman', 'Joker', 'Bane'];
|
|
212
|
+
|
|
213
|
+
const index = names.indexOf('Catwoman');
|
|
214
|
+
|
|
215
|
+
index; // => 1
|
|
216
|
+
</code></pre>
|
|
217
|
+
<p><strong>提示:</strong></p>
|
|
218
|
+
<ul>
|
|
219
|
+
<li>如果找不到该项,则<code>array.indexOf(itemToSearch)</code>返回<code>-1</code></li>
|
|
220
|
+
<li><code>array.findIndex(predicate)</code>是使用<code>predicate</code>函数查找索引的替代方法。</li>
|
|
221
|
+
</ul>
|
|
222
|
+
<h2 id="8-查询数组"><a href="#8-查询数组" class="headerlink" title="8. 查询数组"></a>8. 查询数组</h2><p>**8.1 ****<code>array.every()</code> 方法**</p>
|
|
223
|
+
<p>如果每个项都通过<code>predicate</code> 检查,则<code>array.every(predicate)</code>返回<code>true</code>。</p>
|
|
224
|
+
<p>在每个遍历<code>predicate(item[, index[, array]])</code>上,用参数调用<code>predicate</code> 函数:当前遍历项、索引和数组本身。</p>
|
|
225
|
+
<p>如下所示,确定数组是否只包含偶数:</p>
|
|
226
|
+
<pre><code>const evens = [0, 2, 4, 6];
|
|
227
|
+
const numbers = [0, 1, 4, 6];
|
|
228
|
+
|
|
229
|
+
function isEven(number) {
|
|
230
|
+
return number % 2 === 0;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
evens.every(isEven); // => true
|
|
234
|
+
numbers.every(isEven); // => false
|
|
235
|
+
</code></pre>
|
|
236
|
+
<p>**8.2 ****<code>array.some()</code> 方法**</p>
|
|
237
|
+
<p>如果每个项只要一下通过<code>predicate</code> 检查,则<code>array.every(predicate)</code>返回<code>true</code>。</p>
|
|
238
|
+
<p>在每个遍历<code>predicate(item[, index[, array]])</code>上,用参数调用<code>predicate</code> 函数:当前遍历项、索引和数组本身。</p>
|
|
239
|
+
<p>如下所示:确定数组是否至少包含一个偶数:</p>
|
|
240
|
+
<pre><code>const numbers = [1, 5, 7, 10];
|
|
241
|
+
const odds = [1, 3, 3, 3];
|
|
242
|
+
|
|
243
|
+
function isEven(number) {
|
|
244
|
+
return number % 2 === 0;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
numbers.some(isEven); // => true
|
|
248
|
+
odds.some(isEven); // => false
|
|
249
|
+
</code></pre>
|
|
250
|
+
<h2 id="9-数组的过滤"><a href="#9-数组的过滤" class="headerlink" title="9. 数组的过滤"></a>9. 数组的过滤</h2><p>**9.1 ****<code>array.filter()</code> 方法**</p>
|
|
251
|
+
<p><code>array.filter(predicate)</code>方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。</p>
|
|
252
|
+
<p>在每个遍历<code>predicate(item[, index[, array]])</code>上,用参数调用<code>predicate</code> 函数:当前遍历项、索引和数组本身。</p>
|
|
253
|
+
<p>如下所示:将一个数组过滤为仅包含偶数:</p>
|
|
254
|
+
<pre><code>const numbers = [1, 5, 7, 10];
|
|
255
|
+
|
|
256
|
+
function isEven(number) {
|
|
257
|
+
return number % 2 === 0;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const evens = numbers.filter(isEven);
|
|
261
|
+
|
|
262
|
+
evens; // => [10]
|
|
263
|
+
</code></pre>
|
|
264
|
+
<p><strong>提示:</strong></p>
|
|
265
|
+
<p><code>array.filter()</code> 创建一个新数组,而不改变原始数组。</p>
|
|
266
|
+
<h2 id="10-数组的插入"><a href="#10-数组的插入" class="headerlink" title="10. 数组的插入"></a>10. 数组的插入</h2><p>**10.1 ****<code>array.push()</code> 方法**</p>
|
|
267
|
+
<p><code>array.push(item1 [...,itemN])</code> 方法将一个或多个项追加到数组的末尾,并返回新的长度。</p>
|
|
268
|
+
<p>如下所示,在<code>names</code> 数组的末尾添加 <code>'Joker'</code></p>
|
|
269
|
+
<pre><code>const names = ['Batman'];
|
|
270
|
+
|
|
271
|
+
names.push('Joker');
|
|
272
|
+
|
|
273
|
+
names; // ['Batman', 'Joker']
|
|
274
|
+
</code></pre>
|
|
275
|
+
<p><strong>提示:</strong></p>
|
|
276
|
+
<ul>
|
|
277
|
+
<li><code>array.push()</code> 会改变原数组</li>
|
|
278
|
+
<li><code>array.push(item1, item2, ..., itemN)</code> 可以添加多个元素。</li>
|
|
279
|
+
</ul>
|
|
280
|
+
<p>**10.2 ****<code>array.unshift() </code> 方法**</p>
|
|
281
|
+
<p><code>array.unshift(item1[..., itemN])</code>方法将一个或多个项追加到数组的开头,返回数组的新长度</p>
|
|
282
|
+
<pre><code>const names = ['Batman'];
|
|
283
|
+
|
|
284
|
+
names.unshift('Catwoman');
|
|
285
|
+
|
|
286
|
+
names; // ['Catwoman', 'Batman']
|
|
287
|
+
</code></pre>
|
|
288
|
+
<p><strong>提示:</strong></p>
|
|
289
|
+
<ul>
|
|
290
|
+
<li><code>array.unshift()</code> 会改变原数组</li>
|
|
291
|
+
<li><code>array.unshift(item1, item2, ..., itemN)</code> 可以添加多个元素。</li>
|
|
292
|
+
</ul>
|
|
293
|
+
<p><strong>10.3 展开操作符</strong></p>
|
|
294
|
+
<p>可以通过组合展开操作符和数组字面量以不可变的方式在数组中插入项。</p>
|
|
295
|
+
<p>在数组末尾追加一个项:</p>
|
|
296
|
+
<pre><code>const names = ['Joker', 'Bane'];
|
|
297
|
+
|
|
298
|
+
const names2 = [
|
|
299
|
+
...names,
|
|
300
|
+
'Batman',
|
|
301
|
+
];
|
|
302
|
+
|
|
303
|
+
names2; // => ['Joker', 'Bane', 'Batman'];
|
|
304
|
+
</code></pre>
|
|
305
|
+
<p>在数组的开头追加一个项:</p>
|
|
306
|
+
<pre><code>const names = ['Joker', 'Bane'];
|
|
307
|
+
|
|
308
|
+
const names2 = [
|
|
309
|
+
'Batman',
|
|
310
|
+
...names
|
|
311
|
+
];
|
|
312
|
+
|
|
313
|
+
names2; // => ['Batman', 'Joker', 'Bane'];
|
|
314
|
+
</code></pre>
|
|
315
|
+
<p>在任何索引处插入元素:</p>
|
|
316
|
+
<pre><code>const names = ['Joker', 'Bane'];
|
|
317
|
+
const indexToInsert = 1;
|
|
318
|
+
|
|
319
|
+
const names2 = [
|
|
320
|
+
...names.slice(0, indexToInsert),
|
|
321
|
+
'Batman',
|
|
322
|
+
...names.slice(indexToInsert)
|
|
323
|
+
];
|
|
324
|
+
|
|
325
|
+
names2; // => ['Joker', 'Batman', 'Bane'];
|
|
326
|
+
</code></pre>
|
|
327
|
+
<h2 id="11-删除数组元素"><a href="#11-删除数组元素" class="headerlink" title="11. 删除数组元素"></a>11. 删除数组元素</h2><p>**11.1 ****<code>array.pop()</code> 方法**</p>
|
|
328
|
+
<p><code>array.pop()</code>方法从数组中删除最后一个元素,然后返回该元素。如下所示,删除<code>colors</code>数组的最后一个元素:</p>
|
|
329
|
+
<pre><code>const colors = ['blue', 'green', 'black'];
|
|
330
|
+
|
|
331
|
+
const lastColor = colors.pop();
|
|
332
|
+
|
|
333
|
+
lastColor; // => 'black'
|
|
334
|
+
colors; // => ['blue', 'green']
|
|
335
|
+
</code></pre>
|
|
336
|
+
<p>提示:</p>
|
|
337
|
+
<p><code>array.pop()</code> 会改变原数组。</p>
|
|
338
|
+
<p>**11.2 ****<code>array.shift()</code> 方法**</p>
|
|
339
|
+
<p><code>array.shift()</code>方法从数组中删除第一个元素,然后返回该元素。</p>
|
|
340
|
+
<pre><code>const colors = ['blue', 'green', 'black'];
|
|
341
|
+
|
|
342
|
+
const firstColor = colors.shift();
|
|
343
|
+
|
|
344
|
+
firstColor; // => 'blue'
|
|
345
|
+
colors; // => ['green', 'black']
|
|
346
|
+
</code></pre>
|
|
347
|
+
<p>提示:</p>
|
|
348
|
+
<ul>
|
|
349
|
+
<li><code>array.shift()</code> 会改变原数组。</li>
|
|
350
|
+
<li><code>array.shift()</code> 具<code>有O(n)</code>复杂度。</li>
|
|
351
|
+
</ul>
|
|
352
|
+
<p>**11.3 ****<code>array.splice()</code> 方法**</p>
|
|
353
|
+
<p><code>array.splice(fromIndex[, removeCount[, item1[, item2[, ...]]]])</code>从数组中删除元素,并插入新的元素。</p>
|
|
354
|
+
<p>例如,咱们从索引<code>1</code>处删除<code>2</code>个元素:</p>
|
|
355
|
+
<pre><code>const names = ['张三', '李四', '王五', '赵六']
|
|
356
|
+
|
|
357
|
+
names.splice(1, 2)
|
|
358
|
+
|
|
359
|
+
names // => ["张三", "赵六"]
|
|
360
|
+
</code></pre>
|
|
361
|
+
<p><code>names.splice(1,2)</code>删除元素<code>'张三'</code>和<code>'赵六'</code>。</p>
|
|
362
|
+
<p><code>names.splice()</code> 可以插入新元素,而不是插入已删除的元素。 咱们可以替换索引<code>1</code>处开始的的<code>2</code>个元素,然后插入一个新的元素 <code>'小红'</code>:</p>
|
|
363
|
+
<pre><code>const names = ['张三', '李四', '王五', '赵六']
|
|
364
|
+
|
|
365
|
+
names.splice(1, 2, '小红')
|
|
366
|
+
|
|
367
|
+
names // ["张三", "小红", "赵六"]
|
|
368
|
+
</code></pre>
|
|
369
|
+
<p>提示:</p>
|
|
370
|
+
<ul>
|
|
371
|
+
<li><code>array.splice()</code> 会改变原数组。</li>
|
|
372
|
+
</ul>
|
|
373
|
+
<p><strong>11.4 展开操作符号</strong></p>
|
|
374
|
+
<p>可以通过组合展开操作符和数据字面量以不可变的方式从数组中删除项。</p>
|
|
375
|
+
<pre><code>const names = ['张三', '李四', '王五', '赵六']
|
|
376
|
+
const fromIndex = 1
|
|
377
|
+
const removeCount = 2
|
|
378
|
+
|
|
379
|
+
const newNames = [
|
|
380
|
+
...names.slice(0, fromIndex),
|
|
381
|
+
...names.slice(fromIndex + removeCount)
|
|
382
|
+
]
|
|
383
|
+
|
|
384
|
+
newNames // ["张三", "赵六"]
|
|
385
|
+
</code></pre>
|
|
386
|
+
<h2 id="12-清空数组"><a href="#12-清空数组" class="headerlink" title="12. 清空数组"></a>12. 清空数组</h2><p>**12.1 ****<code>array.length</code>属性**</p>
|
|
387
|
+
<p><code>array.length</code>是保存数组长度的属性。 除此之外,<code>array.length</code>是可写的。</p>
|
|
388
|
+
<p>如果咱们写一个小于当前长度的<code>array.length = newLength</code>,多余的元素从数组中移除。</p>
|
|
389
|
+
<p>如下所示:使用<code>array.length = 0</code>删除数组中的所有项目:</p>
|
|
390
|
+
<pre><code>const colors = ['blue', 'green', 'black'];
|
|
391
|
+
|
|
392
|
+
colors.length = 0;
|
|
393
|
+
|
|
394
|
+
colors; // []
|
|
395
|
+
</code></pre>
|
|
396
|
+
<p>**12.2 ****<code>array.splice()</code> 方法**</p>
|
|
397
|
+
<p><code>array.splice(fromIndex[, removeCount[, item1[, item2[, ...]]]])</code>从数组中删除元素,并插入新的元素。</p>
|
|
398
|
+
<p>如果<code>removeCount</code>参数被省略,那么<code>array.splice()</code>将删除从<code>fromIndex</code>开始的数组的所有元素。咱们使用它来删除数组中的所有元素:</p>
|
|
399
|
+
<pre><code>const colors = ['blue', 'green', 'black'];
|
|
400
|
+
|
|
401
|
+
colors.splice(0);
|
|
402
|
+
|
|
403
|
+
colors; // []
|
|
404
|
+
</code></pre>
|
|
405
|
+
<h2 id="13-填充数组"><a href="#13-填充数组" class="headerlink" title="13. 填充数组"></a>13. 填充数组</h2><p>**13.1 ****<code>array.fill()</code> 方法**</p>
|
|
406
|
+
<p><code>array.fill(value[, fromIndex[, toIndex]])</code>用从<code>fromIndex</code> 到<code>toIndex</code>的值填充数组(不包括<code>toIndex</code>本身)。<code>fromIndex</code>可选参数默认为<code>0</code>,<code>toIndex</code>可选参数默认为<code>array.length</code>。</p>
|
|
407
|
+
<p>例如,使用用零值填充数组:</p>
|
|
408
|
+
<pre><code>const numbers = [1, 2, 3, 4];
|
|
409
|
+
|
|
410
|
+
numbers.fill(0);
|
|
411
|
+
|
|
412
|
+
numbers; // => [0, 0, 0, 0]
|
|
413
|
+
</code></pre>
|
|
414
|
+
<p>不仅如此,还可以使用<code>Array(length).fill(initial)</code>来初始化特定长度和初始值的数组。</p>
|
|
415
|
+
<pre><code>const length = 3;
|
|
416
|
+
const zeros = Array(length).fill(0);
|
|
417
|
+
|
|
418
|
+
zeros; // [0, 0, 0]
|
|
419
|
+
</code></pre>
|
|
420
|
+
<p>提示:</p>
|
|
421
|
+
<ul>
|
|
422
|
+
<li><code>array.splice()</code> 会改变原数组。</li>
|
|
423
|
+
</ul>
|
|
424
|
+
<p>**13.2 ****<code>Array.from()</code> 函数**</p>
|
|
425
|
+
<p><code>Array.from()</code> 有助于初始化带有对象的特定长度的数组:</p>
|
|
426
|
+
<pre><code>const length = 4;
|
|
427
|
+
const emptyObjects = Array.from(Array(length), function() {
|
|
428
|
+
return {};
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
emptyObjects; // [{}, {}, {}, {}]
|
|
432
|
+
</code></pre>
|
|
433
|
+
<h2 id="14-数组的扁平化"><a href="#14-数组的扁平化" class="headerlink" title="14. 数组的扁平化"></a>14. 数组的扁平化</h2><p>**14.1 ****<code>array.flat()</code>方法**</p>
|
|
434
|
+
<p><code>array.flat([depth])</code>方法通过递归扁平属于数组的项直到一定深度来创建新数组。 <code>depth</code>可选参数默认为<code>1</code>:</p>
|
|
435
|
+
<pre><code>const arrays = [0, [1, 3, 5], [2, 4, 6]];
|
|
436
|
+
|
|
437
|
+
const flatArray = arrays.flat();
|
|
438
|
+
|
|
439
|
+
flatArray; // [0, 1, 3, 5, 2, 4, 6]
|
|
440
|
+
</code></pre>
|
|
441
|
+
<p><code>arrays</code> 包含数字和数字数组的混合。 <code>arrays.flat()</code>对数组进行扁平,使其仅包含数字。</p>
|
|
442
|
+
<p><strong>提示:</strong></p>
|
|
443
|
+
<p><code>array.flat()</code> 创建一个新数组,而不会改变原始数组。</p>
|
|
444
|
+
<h2 id="15-数组的排序"><a href="#15-数组的排序" class="headerlink" title="15. 数组的排序"></a>15. 数组的排序</h2><p>**15.1 ****<code>array.sort()</code> 方法**</p>
|
|
445
|
+
<p><code>array.sort([compare])</code>方法对数组的元素进行排序。</p>
|
|
446
|
+
<p>可选参数<code>compare(a, b)</code>是一个自定义排序顺的回调函数。如果比较<code>compare(a, b)</code>返回的结果:</p>
|
|
447
|
+
<ul>
|
|
448
|
+
<li>如果 <code>a</code>小于<code>b</code>,在排序后的数组中<code>a</code>应该出现在<code>b</code>之前,就返回一个小于<code>0</code>的值。</li>
|
|
449
|
+
<li>如果<code>a</code>等于<code>b</code>,就返回<code>0</code>。</li>
|
|
450
|
+
<li>如果<code>a</code>大于<code>b</code>,就返回一个大于<code>0</code>的值。</li>
|
|
451
|
+
</ul>
|
|
452
|
+
<p>如下所示,对数组 <code>numbers</code> 时行排序</p>
|
|
453
|
+
<pre><code>const numbers = [4, 3, 1, 2];
|
|
454
|
+
|
|
455
|
+
numbers.sort();
|
|
456
|
+
|
|
457
|
+
numbers; // => [1, 2, 3, 4]
|
|
458
|
+
</code></pre>
|
|
459
|
+
<p><code>numbers.sort()</code> 以升序对数字进行排序。</p>
|
|
460
|
+
<p>使用比较函数,让偶数排在奇数前面:</p>
|
|
461
|
+
<pre><code>const numbers = [4, 3, 1, 2];
|
|
462
|
+
|
|
463
|
+
function compare(n1, n2) {
|
|
464
|
+
if (n1 % 2 === 0 && n2 % 2 !== 0) {
|
|
465
|
+
return -1;
|
|
466
|
+
}
|
|
467
|
+
if (n1 % 2 !== 0 && n2 % 2 === 0) {
|
|
468
|
+
return 1;
|
|
469
|
+
}
|
|
470
|
+
return 0;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
numbers.sort(compare);
|
|
474
|
+
|
|
475
|
+
numbers; // => [4, 2, 3, 1]
|
|
476
|
+
</code></pre>
|
|
477
|
+
<p><strong>提示:</strong></p>
|
|
478
|
+
<ul>
|
|
479
|
+
<li><code>array.sort()</code> 会改变原数组。</li>
|
|
480
|
+
</ul>
|
|
481
|
+
<p>原文:<a target="_blank" rel="noopener" href="https://dmitripavlutin.com/operations-on-arrays-javascript/">https://dmitripavlutin.com/operations-on-arrays-javascript/</a></p>
|
|
482
|
+
<p>更多<a target="_blank" rel="noopener" href="https://www.html.cn/">前端开发</a>知识,请查阅 HTML中文网 !!</p>
|
|
483
|
+
<p>以上就是JavaScript中数组的15种常见操作(备忘单)的详细内容,更多请关注html中文网其它相关文章!</p>
|
|
484
|
+
</div><iframe src="/donate/?AliPayQR=https://store.xuehuayu.cn/store/public/alipay_qr.jpg&WeChatQR=https://store.xuehuayu.cn/store/public/wechat_qr.png&UnionPayQR=null&GitHub=null&BTCQR=null&BTCKEY=null&PayPal=https://www.paypal.com/paypalme/OrdinaryPerson" style="overflow-x:hidden; overflow-y:hidden; border:0xp none #fff; min-height:240px; width:100%;" frameborder="0" scrolling="no"></iframe><div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者:</strong>雪花雨</li><li class="post-copyright-link"><strong>本文链接:</strong><a href="/article/1230.html">https://xuehuayu.cn/article/1230.html</a></li><li class="post-copyright-license"><strong>版权声明:</strong>① 标为原创的文章为博主原创,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接。② 转载文章来自网络,已标明出处,<a href="mailto:cainiaoblog@126.com">侵删</a>。</li></ul></div><br><div class="tags"><a href="/tags/%E8%BD%AC%E8%BD%BD/">转载</a></div><div class="post-nav"><a class="pre" href="/article/39637.html">常见React.js问题(附解决方案)</a><a class="next" href="/article/38269.html">前端面试时面试官想要听到什么答案(关于一些Vue的问题)</a></div><div id="vcomment"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@waline/client@v2/dist/waline.min.css"><script src="//cdn.jsdelivr.net/npm/@waline/client@v2/dist/waline.js"></script><script>const origin = window.location.origin
|
|
485
|
+
const serverURL = origin.includes('cainiaoblog') ? 'https://cmts.cainiaoblog.cn' : 'https://cmts.xuehuayu.cn'
|
|
486
|
+
const locale = {
|
|
487
|
+
placeholder: '请正确填写昵称和邮箱,方便接收回复通知~',
|
|
488
|
+
sofa: '沙发空缺中,还不快抢~',
|
|
489
|
+
};
|
|
490
|
+
Waline.init({
|
|
491
|
+
el: '#vcomment',
|
|
492
|
+
serverURL: serverURL,
|
|
493
|
+
pageSize: '20',
|
|
494
|
+
visitor: true, // 阅读量统计
|
|
495
|
+
requiredMeta: ['nick', 'mail'],
|
|
496
|
+
emoji: [
|
|
497
|
+
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/weibo',
|
|
498
|
+
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/qq',
|
|
499
|
+
'https://cdn.jsdelivr.net/gh/walinejs/emojis@1.0.0/tw-emoji',
|
|
500
|
+
],
|
|
501
|
+
locale,
|
|
502
|
+
pageview: true
|
|
503
|
+
})
|
|
504
|
+
</script></div></div></div><div class="pure-u-1 pure-u-md-1-4 fixed-search hidden_mid_and_down"><div id="sidebar"><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/search.min.js"></script><div class="widget"><div class="widget-search"><input class="search" type="radio" name="search" value="baidu" id="baidu" checked="checked"/><label class="label" for="baidu" title="百度全站搜索">百度</label><input class="search" type="radio" name="search" value="google" id="google"/><label class="label" for="google" title="谷歌全站搜索">谷歌</label><input class="search" type="radio" name="search" value="self" id="self"/><label class="label" for="self" title="使用站内搜索">站内</label></div><div class="widget" id="search"><form class="search-form" action="//www.baidu.com/baidu" method="get" accept-charset="utf-8" target="_blank"><input type="search" name="word" maxlength="30" placeholder="百度全站搜索"/><input type="hidden" name="si" value="xuehuayu.cn"/><input type="hidden" name="cl" value="3"/><input type="hidden" name="ct" value="2097152"/><input type="hidden" name="s" value="on"/><input class="search-submit" type="submit" value=""/></form></div></div><script>$('input[type=radio][name=search]').change(function() {
|
|
505
|
+
var val = $(this).val()
|
|
506
|
+
var self = '<div class="search-form"><input id="local-search-input" placeholder="站内搜索,首次慢" type="search" name="q" results="0"><input class="search-submit" type="submit" value=""/><div id="local-search-result"></div></div>'
|
|
507
|
+
var google = '<form class="search-form" action="//www.google.com/search" method="get" accept-charset="utf-8" target="_blank"><input type="search" name="q" maxlength="30" placeholder="谷歌全站搜索"><input type="hidden" name="sitesearch" value="xuehuayu.cn"><input class="search-submit" type="submit" value=""/></form>'
|
|
508
|
+
var baidu = '<form class="search-form" action="//www.baidu.com/baidu" method="get" accept-charset="utf-8" target="_blank"><input type="search" name="word" maxlength="30" placeholder="百度全站搜索"><input type="hidden" name="si" value="xuehuayu.cn"><input type="hidden" name="cl" value="3"><input type="hidden" name="ct" value="2097152"><input type="hidden" name="s" value="on"><input class="search-submit" type="submit" value=""/></form>'
|
|
509
|
+
|
|
510
|
+
if (val === 'self') {
|
|
511
|
+
$('#search').html(self)
|
|
512
|
+
var search_path = 'search.xml';
|
|
513
|
+
if (search_path.length == 0) {
|
|
514
|
+
search_path = 'search.xml';
|
|
515
|
+
}
|
|
516
|
+
var path = 'https://cdn.staticaly.com/gh/npljy/npljy.github.io/main/' + search_path;
|
|
517
|
+
searchFunc(path, 'local-search-input', 'local-search-result');
|
|
518
|
+
} else if (val === 'baidu') {
|
|
519
|
+
$('#search').html(baidu)
|
|
520
|
+
} else if (val === 'google') {
|
|
521
|
+
$('#search').html(google)
|
|
522
|
+
}
|
|
523
|
+
})</script><div class="widget widget-wxmp"><img alt="微信公众号" width="100%" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/mp-mini.jpg"/></div><div class="widget widget-categories"><div class="widget-title"><i class="fa fa-folder-o"> 分类</i></div><ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/CDN/">CDN</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/IDE/">IDE</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/git/">git</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/hexo/">hexo</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%85%AC%E5%85%B1%E8%8A%82%E6%97%A5/">公共节日</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%88%86%E4%BA%AB/">分享</a><span class="category-list-count">29</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/">前端</a><span class="category-list-count">191</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/%E6%B5%8F%E8%A7%88%E5%99%A8/">浏览器</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%89%8D%E7%AB%AF/%E6%B5%8F%E8%A7%88%E5%99%A8/%E7%BD%91%E7%BB%9C%E5%8D%8F%E8%AE%AE/">网络协议</a><span class="category-list-count">1</span></li></ul></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%8D%9A%E5%AE%A2/">博客</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B7%A5%E5%85%B7/">工具</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B9%BF%E5%91%8A%E8%BF%87%E6%BB%A4/">广告过滤</a><span class="category-list-count">8</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%B9%BF%E5%91%8A%E8%BF%87%E6%BB%A4/%E8%A7%86%E9%A2%91%E7%94%B5%E8%A7%86%E5%B0%8F%E8%AF%B4%E6%BC%AB%E7%94%BB%E8%B5%84%E8%AE%AF%E9%9F%B3%E4%B9%90/">视频电视小说漫画资讯音乐</a><span class="category-list-count">2</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%AD%A3%E5%88%99/">正则</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%B5%8F%E8%A7%88%E5%99%A8/">浏览器</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%AE%97%E6%B3%95/">算法</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%B5%84%E8%AE%AF/">资讯</a><span class="category-list-count">16</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BD%BB%E6%9D%BE%E4%B8%80%E5%88%BB/">轻松一刻</a><span class="category-list-count">1</span></li></ul></div><div class="widget widget-tags"><div class="widget-title"><i class="fa fa-star-o"> 标签</i></div><div class="tagcloud"><a href="/tags/%E5%8E%9F%E5%88%9B/" style="font-size: 15px;">原创</a> <a href="/tags/%E8%BD%AC%E8%BD%BD/" style="font-size: 15px;">转载</a> <a href="/tags/%E6%89%8B%E5%86%99/" style="font-size: 15px;">手写</a> <a href="/tags/%E7%96%AB%E6%83%85/" style="font-size: 15px;">疫情</a> <a href="/tags/%E5%A8%B1%E4%B9%90/" style="font-size: 15px;">娱乐</a></div></div><div class="widget widget-recent-posts"><div class="widget-title"><i class="fa fa-file-o"> 最近文章</i></div><ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/article/c5576490.html">面试准备</a></li><li class="post-list-item"><a class="post-list-link" href="/article/84c9ccd6.html">精读《SolidJS》</a></li><li class="post-list-item"><a class="post-list-link" href="/article/53803e6.html">git提交注释规范</a></li><li class="post-list-item"><a class="post-list-link" href="/article/a4145266.html">前端数组拍平flat一行代码</a></li><li class="post-list-item"><a class="post-list-link" href="/article/bedea419.html">一道前端this面试题</a></li><li class="post-list-item"><a class="post-list-link" href="/article/bf7e7421.html">git多账号配置</a></li></ul></div><div class="widget widget-links"><div class="widget-title"><i class="fa fa-external-link"> 友情链接</i></div><ul></ul><a href="https://github.com/npljy?utm_source=xuehuayu.cn" title="GitHub" target="_blank">GitHub</a><ul></ul><a href="https://laonongmin.online?utm_source=xuehuayu.cn" title="看看" target="_blank">看看</a><ul></ul><a href="https://cainiaoblog.cn?utm_source=xuehuayu.cn" title="菜鸟博客" target="_blank">菜鸟博客</a><ul></ul><a href="mailto:cainiaoblog@126.com" title="友链联系" target="_blank">友链联系</a></div></div></div><div class="pure-u-1 pure-u-md-3-4"><div id="footer"><div class="flex-block justify-center align-center flex-wrap"><a class="gxba-link" id="gxba" rel="nofollow" target="_blank" href="http://beian.miit.gov.cn/">京ICP备20007647号-2</a><a class="gaba-link" id="gaba-link" rel="nofollow" target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=11010802031264"><img class="nofancybox" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/gaba.png" alt=""/><span id="gaba">京公网安备 11010802031264号</span><span style="padding-right: 10px;"></span></a><span>Copyright © 2023 </span><a href="/." rel="nofollow">前端壹菜鸟. </a><script>(function(){
|
|
524
|
+
var cnb =window.location.origin.indexOf('cainiaoblog') !==-1
|
|
525
|
+
if (cnb) {
|
|
526
|
+
var gxba =document.getElementById('gxba')
|
|
527
|
+
var gaba =document.getElementById('gaba')
|
|
528
|
+
var gabaLink =document.getElementById('gaba-link')
|
|
529
|
+
gxba.innerText ='京ICP备20007647号-1'
|
|
530
|
+
gaba.innerText ='京公网安备 11010802031254号'
|
|
531
|
+
gabaLink.setAttribute('href','http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=11010802031254')
|
|
532
|
+
}
|
|
533
|
+
})()</script></div><div class="flex-block justify-center align-center flex-wrap"><a rel="nofollow" target="_blank" href="https://v6.51.la/s/Guz3lwHCsVme9Cu"><div class="busuanzi_container" id="busuanzi_container"><span class="busuanzi_container_site_pv" id="busuanzi_container_site_pv">访问量:<span id="busuanzi_value_site_pv"><i class="fa fa-spinner"></i></span></span><span class="busuanzi_container_site_uv" id="busuanzi_container_site_uv">访客数:<span id="busuanzi_value_site_uv"><i class="fa fa-spinner"></i></span></span><span style="padding-right: 10px;"></span></div></a><a rel="nofollow" target="_blank" href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral"><span style="font-weight:bold;letter-spacing: 1px;">本网站由 </span><img class="nofancybox" height="32" style="vertical-align: middle" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/img/upy_logo.min.svg" alt=""/><span style="font-weight:bold;letter-spacing: 1px;">提供CDN加速/云存储服务</span></a><span style="padding-right: 10px;"></span><a rel="nofollow" style="font-size:18px;font-weight:bold;vertical-align: middle" target="_blank" title="注册就送代金券可直接使用" href="https://console.upyun.com/register/?invite=HyDsjZHIL">注册</a></div></div></div></div><a class="show" id="rocket" href="#top"></a><div class="darkmode-toggle">🌓</div><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/totop.min.js" async></script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/dark.min.js" async></script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/codeblock-resizer.min.js"></script><script type="text/javascript" src="//cdn.staticaly.com/gh/npljy/npljy.github.io/main/js/smartresize.min.js"></script></div></body></html>
|