reduce

reduce

  1. 兩個參數(shù)贪惹,第一個是回調(diào)函數(shù)底扳,第二個是初始值

    1. 回調(diào)函數(shù)里有四個參數(shù):

      1. pre累加器

      2. 當(dāng)前數(shù)組元素

      3. 索引

      4. 數(shù)組

        [圖片上傳失敗...(image-7b3874-1645235847602)]

常見用法

  1. 數(shù)組所有值求和

    1. <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="javascript" cid="n117" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit; background-position: inherit; background-repeat: inherit;">var arr = [1, 2, 3, 4, 5, 5]
      function Sum(arr) {
      return arr.reduce((pre,cur,index)=>pre+cur,0)
      }
      console.log(Sum(arr));</pre>
  2. 數(shù)組中對象求和

    1. <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="javascript" cid="n124" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit; background-position: inherit; background-repeat: inherit;">var arr = [
      {
      x:5
      },
      {
      x:5
      },
      {
      x:5
      },

      ]
      function ObjSum(arr) {
      return arr.reduce((pre,cur)=>pre+cur.x,0)
      }
      console.log(ObjSum(arr));</pre>

  3. 將二維數(shù)組轉(zhuǎn)化成一維數(shù)組

    1. <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="javascript" cid="n131" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit; background-position: inherit; background-repeat: inherit;">var arr = [[1], [2], [3], [1]]
      function arrO(arr) {
      return arr.reduce((pre, cur) => [...pre,...cur],[])
      }
      console.log(arrO(arr));</pre>
  4. 求數(shù)組中出現(xiàn)次數(shù)最多的元素:用in 關(guān)鍵字判斷對象中是否有這個屬性 記得屬性加引號。

    1. <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="javascript" cid="n107" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit; background-position: inherit; background-repeat: inherit;">var a = ['1111', '1111', '22', '2', '34', '33333', '33333']
      function ObyO(arr) {
      return arr.reduce((pre, cur) => {
      if (cur in pre) {
      pre[cur]++
      } else {
      pre[cur]=1
      }
      return pre
      },{})
      }
      console.log(ObyO(a));</pre>
  5. 給數(shù)組里的對象分類

    1. <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n100" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit; background-position: inherit; background-repeat: inherit;">var obj =[
      {name:'Alice',age:20},
      {name:'Yep',age:21},
      {name:'ls',age:22},
      {name:'zs',age:23},
      ]
      function gui(obj, fanlei) {
      return obj.reduce((pre, cur) => {
      var key = cur[fanlei]
      if (!pre[key]) {
      pre[key]=[]
      }
      pre[key].push(cur)
      return pre
      },{})
      }
      console.log(gui(obj,'age'));</pre>
  6. 結(jié)構(gòu)數(shù)組里的對象遏暴,并且合成一個數(shù)組

    1. <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="javascript" cid="n92" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit; background-position: inherit; background-repeat: inherit;"> var obj = [
      {
      name: 'ls',
      books: ['asd', 'sad', 'asddasd'],
      age:20
      },
      {
      name: 'za',
      books: ['qasd', 'qsad', 'qasddasd'],
      age:20
      },
      {
      name: 'pa',
      books: ['pasd', 'pqsad', 'pqasddasd'],
      age:20
      },
      ]
      function abc(obj) {
      return obj.reduce((pre, cur, index) => {
      return [...pre,...cur.books]
      },[])
      }
      console.log(abc(obj));</pre>
  7. 數(shù)組去重

    1. <pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="javascript" cid="n84" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 0px; width: inherit; background-position: inherit; background-repeat: inherit;">var a = [1, 2, 1, 2, 3, 2, 1, 3, 4, 5, 6, 7, 5, 4, 6, 7]
      function Mysort(arr) {
      for (var i = 0; i < arr.length; i++){
      for (var j = 0; j < arr.length; j++){
      if (arr[j] > arr[j + 1]) {
      var temp = arr[j]
      arr[j] = arr[j + 1]
      arr[j+1]=temp
      }
      }
      }
      return arr
      }
      Mysort(a)

      function Myslice(arr) {
      return arr.reduce((pre, cur, index) => {
      if (pre.length == 0 || pre[pre.length - 1] !== cur) {
      pre.push(cur)
      }
      return pre
      },[])
      }
      console.log(Myslice(a))</pre>

  8. promise:暫緩

  9. 管道函數(shù):暫緩

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末侄刽,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子朋凉,更是在濱河造成了極大的恐慌州丹,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件杂彭,死亡現(xiàn)場離奇詭異墓毒,居然都是意外死亡,警方通過查閱死者的電腦和手機亲怠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門所计,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人团秽,你說我怎么就攤上這事主胧。” “怎么了习勤?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵踪栋,是天一觀的道長。 經(jīng)常有香客問我姻报,道長己英,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任吴旋,我火速辦了婚禮损肛,結(jié)果婚禮上厢破,老公的妹妹穿的比我還像新娘。我一直安慰自己治拿,他們只是感情好摩泪,可當(dāng)我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著劫谅,像睡著了一般见坑。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上捏检,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天荞驴,我揣著相機與錄音,去河邊找鬼贯城。 笑死熊楼,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的能犯。 我是一名探鬼主播鲫骗,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼踩晶!你這毒婦竟也來了执泰?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤渡蜻,失蹤者是張志新(化名)和其女友劉穎术吝,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體茸苇,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡顿苇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了税弃。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡凑队,死狀恐怖则果,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情漩氨,我是刑警寧澤西壮,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站叫惊,受9級特大地震影響款青,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜霍狰,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一抡草、第九天 我趴在偏房一處隱蔽的房頂上張望饰及。 院中可真熱鬧,春花似錦康震、人聲如沸燎含。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽屏箍。三九已至,卻和暖如春橘忱,著一層夾襖步出監(jiān)牢的瞬間赴魁,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工钝诚, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留颖御,地道東北人。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓敲长,卻偏偏與公主長得像郎嫁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子祈噪,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容