JavaScript中Reduce10個(gè)常用場(chǎng)景技巧
不知道大家平常用 Reduce 多不多,反正本瓜用的不多。但實(shí)際上,Reduce 能做的,比我們能想到的要多得多,本篇帶來 10 個(gè)Reduce 常用場(chǎng)景和技巧,一定有你不知道~
沖ヾ(?°∇°?)??
累加/累積
累加我們可能是最熟悉 Reduce 的一種用法,除此之外,還可以用做累積。
// adder const sum = (...nums) => { return nums.reduce((sum, num) => sum + num); }; console.log(sum(1, 2, 3, 4, 10)); // 20 // accumulator const accumulator = (...nums) => { return nums.reduce((acc, num) => acc * num); }; console.log(accumulator(1, 2, 3)); // 6
求最大/最小值
如果你用原生 api 求最大/最小值,無(wú)可厚非,Reduce 也能實(shí)現(xiàn)同樣的效果。
const array = [-1, 10, 6, 5]; const max = Math.max(...array); // 10 const min = Math.min(...array); // -1
const array = [-1, 10, 6, 5]; const max = array.reduce((max, num) => (max > num ? max : num)); const min = array.reduce((min, num) => (min < num ? min : num));
格式化搜索參數(shù)
獲取 url 上的參數(shù)是我們經(jīng)常面臨的需求,用 forEach 遍歷可以,用 Reduce 累加更可以,這樣可以減少聲明 query 對(duì)象。
// url https://qianlongo.github.io/vue-demos/dist/index.html?name=fatfish&age=100#/home // format the search parameters { "name": "fatfish", "age": "100" }
const parseQuery = () => { const search = window.location.search; let query = {}; search .slice(1) .split("&") .forEach((it) => { const [key, value] = it.split("="); query[key] = decodeURIComponent(value); }); return query; };
const parseQuery = () => { const search = window.location.search; return search .slice(1) .split("&") .reduce((query, it) => { const [key, value] = it.split("="); query[key] = decodeURIComponent(value); return query; }, {}); };
反序列化搜索參數(shù)
有了獲取 url 參數(shù),就有把參數(shù)重新掛在到 url 上面,好用,收藏。
const searchObj = { name: "fatfish", age: 100, // ... }; const link = `https://medium.com/?name=${searchObj.name}&age=${searchObj.age}`; // https://medium.com/?name=fatfish&age=100
const stringifySearch = (search = {}) => { return Object.entries(search) .reduce( (t, v) => `${t}${v[0]}=${encodeURIComponent(v[1])}&`, Object.keys(search).length ? "?" : "" ) .replace(/&$/, ""); }; const search = stringifySearch({ name: "fatfish", age: 100, }); const link = `https://medium.com/${search}`; console.log(link); // https://medium.com/?name=fatfish&age=100
拉平嵌套數(shù)組
我們都會(huì)用 .flat(Infinity) 無(wú)限拉平所有多維數(shù)組成一維數(shù)組,只用 reduce 和 flat 也是可以做到這一點(diǎn)的。
const array = [1, [2, [3, [4, [5]]]]]; // expected output [ 1, 2, 3, 4, 5 ] const flatArray = array.flat(Infinity); // [1, 2, 3, 4, 5]
const flat = (array) => { return array.reduce( (acc, it) => acc.concat(Array.isArray(it) ? flat(it) : it), [] ); }; const array = [1, [2, [3, [4, [5]]]]]; const flatArray = flat(array); // [1, 2, 3, 4, 5]
實(shí)現(xiàn) flat
如果想實(shí)現(xiàn) flat,用 reduce 沒錯(cuò)了,又是一個(gè)手寫原生 api 內(nèi)部實(shí)現(xiàn),妥妥的剛。
// Expand one layer by default Array.prototype.flat2 = function (n = 1) { const len = this.length let count = 0 let current = this if (!len || n === 0) { return current } // Confirm whether there are array items in current const hasArray = () => current.some((it) => Array.isArray(it)) // Expand one layer after each cycle while (count++ < n && hasArray()) { current = current.reduce((result, it) => { result = result.concat(it) return result }, []) } return current } const array = [ 1, [ 2, [ 3, [ 4, [ 5 ] ] ] ] ] // Expand one layer console.log(array.flat()) // [ 1, 2, [ 3, [ 4, [ 5 ] ] ] ] console.log(array.flat2()) // [ 1, 2, [ 3, [ 4, [ 5 ] ] ] ] // Expand all console.log(array.flat(Infinity)) console.log(array.flat2(Infinity))
數(shù)組去重
數(shù)組去重,用 reduce 竟然也可以,寫法如下:
const array = [ 1, 2, 1, 2, -1, 10, 11 ] const uniqueArray1 = [ ...new Set(array) ] const uniqueArray2 = array.reduce((acc, it) => acc.includes(it) ? acc : [ ...acc, it ], [])
數(shù)組計(jì)數(shù)
將數(shù)組的項(xiàng)進(jìn)行計(jì)數(shù),返回一個(gè) map,分別是每個(gè)項(xiàng)重復(fù)的次數(shù),reduce 一行代碼搞定,收藏!
const count = (array) => { return array.reduce((acc, it) => (acc.set(it, (acc.get(it) || 0) + 1), acc), new Map()) } const array = [ 1, 2, 1, 2, -1, 0, '0', 10, '10' ] console.log(count(array)) // Map(7) {1 => 2, 2 => 2, -1 => 1, 0 => 1, '0' => 1, …}
獲取對(duì)象多個(gè)屬性
獲取對(duì)象的多個(gè)屬性,然后賦給新的對(duì)象,比較笨的做法如下:
// There is an object with many properties const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 // ... } // We just want to get some properties above it to create a new object const newObj = { a: obj.a, b: obj.b, c: obj.c, d: obj.d // ... } // Do you think this is too inefficient?
用 Reduce 這樣解決,就顯得明智了許多:
const getObjectKeys = (obj = {}, keys = []) => { return Object.keys(obj).reduce((acc, key) => (keys.includes(key) && (acc[key] = obj[key]), acc), {}); } const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 // ... } const newObj = getObjectKeys(obj, [ 'a', 'b', 'c', 'd' ]) console.log(newObj)
反轉(zhuǎn)字符串
除了 reverse 做數(shù)組的翻轉(zhuǎn),Reduce 也可以,再加上 split,就可以反轉(zhuǎn)字符串啦。
const reverseString = (string) => { return string.split("").reduceRight((acc, s) => acc + s) } const string = 'fatfish' console.log(reverseString(string)) // hsiftaf
以上就是JavaScript中Reduce10個(gè)常用場(chǎng)景和技巧的詳細(xì)內(nèi)容,更多關(guān)于JavaScript Reduce技巧的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序 textarea 詳解及簡(jiǎn)單使用方法
這篇文章主要介紹了微信小程序 textarea 詳解及簡(jiǎn)單使用方法的相關(guān)資料,這里附有實(shí)現(xiàn)實(shí)例代碼,及解決textarea沒有bindchange事件,無(wú)法在輸入時(shí)給變量賦值的方法, 需要的朋友可以參考下2016-12-12微信小程序 保留小數(shù)(toFixed)詳細(xì)介紹
這篇文章主要介紹了 微信小程序 保留小數(shù)(toFixed)詳細(xì)介紹的相關(guān)資料,這里附有實(shí)例,幫助大家學(xué)習(xí)參考此部分知識(shí),需要的朋友可以參考下2016-11-11Css-In-Js實(shí)現(xiàn)classNames庫(kù)源碼解讀
這篇文章主要為大家介紹了Css-In-Js實(shí)現(xiàn)classNames庫(kù)源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12js基礎(chǔ)語(yǔ)法與maven項(xiàng)目配置教程案例
本篇文章介紹了幾個(gè)javascript的基本語(yǔ)法和maven的配置教程。想學(xué)習(xí)javascript和maven的朋友們可以參考一下,希望能給你帶來幫助2021-07-07JavaScript單例模式能不能去實(shí)例只留單原理解析
這篇文章主要為大家介紹了JavaScript單例模式能不能去實(shí)例只留單原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12微信小程序 基礎(chǔ)組件與導(dǎo)航組件詳細(xì)介紹
這篇文章主要介紹了微信小程序 基礎(chǔ)組件與導(dǎo)航組件詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02websocket心跳重連實(shí)現(xiàn)探索(npm:websocket-heartbeat-js)
這篇文章主要為大家介紹了websocket心跳重連實(shí)現(xiàn)探索(npm:websocket-heartbeat-js),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07