Javascript中數組方法reduce的妙用之處 將小數組展開成大數組

Javascript數組方法中,相比mapfilterforEach等常用的迭代方法,reduce常常被我們所忽略,今天一起來探究一下reduce在我們實戰開發當中,能有哪些妙用之處,下面從reduce語法開始介紹。

語法

array.reduce(function(accumulator, arrayElement, currentIndex, arr), initialValue)

若傳入初始值,accumulator首次迭代就是初始值,否則就是數組的第一個元素;後續迭代中將是上一次迭代函數返回的結果。所以,假如數組的長度爲n,如果傳入初始值,迭代次數爲n;否則爲n-1。

比如實現數組 arr = [1,2,3,4] 求數組的和

let arr = [1,2,3,4];
arr.reduce(function(pre,cur){return pre + cur}); // return 10

實際上reduce還有很多重要的用法,這是因爲累加器的值可以不必爲簡單類型(如數字或字符串),它也可以是結構化類型(如數組或對象),這使得我們可以用它做一些其他有用的事情,比如:

  • 將數組轉換爲對象
  • 展開更大的數組
  • 在一次遍歷中進行兩次計算
  • 將映射和過濾函數組合
  • 按順序運行異步函數

將數組轉化爲對象

在實際業務開發中,你可能遇到過這樣的情況,後臺接口返回的數組類型,你需要將它轉化爲一個根據id值作爲key,將數組每項作爲value的對象進行查找。

例如:

const userList = [
  {
    id: 1,
    username: 'john',
    sex: 1,
    email: '[email protected]'
  },
  {
    id: 2,
    username: 'jerry',
    sex: 1,
    email: '[email protected]'
  },
  {
    id: 3,
    username: 'nancy',
    sex: 0,
    email: ''
  }
];

如果你用過lodash這個庫,使用_.keyBy這個方法就能進行轉換,但用reduce也能實現這樣的需求。

function keyByUsernameReducer(acc, person) {
    return {...acc, [person.id]: person};
}
const userObj = peopleArr.reduce(keyByUsernameReducer, {});
console.log(userObj);

將小數組展開成大數組

試想這樣一個場景,我們將一堆純文本行讀入數組中,我們想用逗號分隔每一行,生成一個更大的數組名單。

const fileLines = [
    'Inspector Algar,Inspector Bardle,Mr. Barker,Inspector Barton',
    'Inspector Baynes,Inspector Bradstreet,Inspector Sam Brown',
    'Monsieur Dubugue,Birdy Edwards,Inspector Forbes,Inspector Forrester',
    'Inspector Gregory,Inspector Tobias Gregson,Inspector Hill',
    'Inspector Stanley Hopkins,Inspector Athelney Jones'
];

function splitLineReducer(acc, line) {
    return acc.concat(line.split(/,/g));
}
const investigators = fileLines.reduce(splitLineReducer, []);
console.log(investigators);
// [
//   "Inspector Algar",
//   "Inspector Bardle",
//   "Mr. Barker",
//   "Inspector Barton",
//   "Inspector Baynes",
//   "Inspector Bradstreet",
//   "Inspector Sam Brown",
//   "Monsieur Dubugue",
//   "Birdy Edwards",
//   "Inspector Forbes",
//   "Inspector Forrester",
//   "Inspector Gregory",
//   "Inspector Tobias Gregson",
//   "Inspector Hill",
//   "Inspector Stanley Hopkins",
//   "Inspector Athelney Jones"
// ]

我們從長度爲5的數組開始,最後得到一個長度爲16的數組。

另一種常見增加數組的情況是flatMap,有時候我們用map方法需要將二級數組展開,這時可以用reduce實現扁平化

例如:

Array.prototype.flatMap = function(f) {
    const reducer = (acc, item) => acc.concat(f(item));
    return this.reduce(reducer, []);
}

const arr = ["今天天氣不錯", "", "早上好"]

const arr1 = arr.map(s => s.split(""))
// [["今", "天", "天", "氣", "不", "錯"],[""],["早", "上", "好"]]

const arr2 = arr.flatMap(s => s.split(''));
// ["今", "天", "天", "氣", "不", "錯", "", "早", "上", "好"]

在一次遍歷中進行兩次計算

有時我們需要對數組進行兩次計算。例如,我們可能想要計算數字列表的最大值和最小值。我們可以通過兩次通過這樣做:

const readings = [0.3, 1.2, 3.4, 0.2, 3.2, 5.5, 0.4];
const maxReading = readings.reduce((x, y) => Math.max(x, y), Number.MIN_VALUE);
const minReading = readings.reduce((x, y) => Math.min(x, y), Number.MAX_VALUE);
console.log({minReading, maxReading});
// {minReading: 0.2, maxReading: 5.5}

這需要遍歷我們的數組兩次。但是,有時我們可能不想這樣做。因爲.reduce()讓我們返回我們想要的任何類型,我們不必返回數字。我們可以將兩個值編碼到一個對象中。然後我們可以在每次迭代時進行兩次計算,並且只遍歷數組一次:

const readings = [0.3, 1.2, 3.4, 0.2, 3.2, 5.5, 0.4];
function minMaxReducer(acc, reading) {
    return {
        minReading: Math.min(acc.minReading, reading),
        maxReading: Math.max(acc.maxReading, reading),
    };
}
const initMinMax = {
    minReading: Number.MAX_VALUE,
    maxReading: Number.MIN_VALUE,
};
const minMax = readings.reduce(minMaxReducer, initMinMax);
console.log(minMax);
// {minReading: 0.2, maxReading: 5.5}

將映射和過濾合併爲一個過程

還是先前那個用戶列表,我們希望找到沒有電子郵件地址的人的用戶名,返回它們用戶名用逗號拼接的字符串。一種方法是使用兩個單獨的操作:

  • 獲取過濾無電子郵件後的條目
  • 獲取用戶名並拼接

將它們放在一起可能看起來像這樣:

function notEmptyEmail(x) {
   return !!x.email
}

function notEmptyEmailUsername(a, b) {
    return a ? `${a},${b.username}` : b.username
}

const userWithEmail = userList.filter(notEmptyEmail);
const userWithEmailFormatStr = userWithEmail.reduce(notEmptyEmailUsername, '');

console.log(userWithEmailFormatStr);
// 'john,jerry'

現在,這段代碼是完全可讀的,對於小的樣本數據不會有性能問題,但是如果我們有一個龐大的數組呢?如果我們修改我們的reducer回調,那麼我們可以一次完成所有事情:

function notEmptyEmail(x) {
   return !!x.email
}

function notEmptyEmailUsername(usernameAcc, person){
    return (notEmptyEmail(person))
        ? (usernameAcc ? `${usernameAcc},${person.username}` : `${person.username}`) : usernameAcc;
}

const userWithEmailFormatStr = userList.reduce(notEmptyEmailUsername, '');

console.log(userWithEmailFormatStr);
// 'john,jerry'

在這個版本中,我們只遍歷一次數組,一般建議使用filtermap的組合,除非發現性能問題,才推薦使用reduce去做優化。

按順序運行異步函數

我們可以做的另一件事.reduce()是按順序運行promises(而不是並行)。如果您對API請求有速率限制,或者您需要將每個prmise的結果傳遞到下一個promise,reduce可以幫助到你。

舉一個例子,假設我們想要爲userList數組中的每個人獲取消息。

function fetchMessages(username) {
    return fetch(`https://example.com/api/messages/${username}`)
        .then(response => response.json());
}

function getUsername(person) {
    return person.username;
}

async function chainedFetchMessages(p, username) {
    // In this function, p is a promise. We wait for it to finish,
    // then run fetchMessages().
    const obj  = await p;
    const data = await fetchMessages(username);
    return { ...obj, [username]: data};
}

const msgObj = userList
    .map(getUsername)
    .reduce(chainedFetchMessages, Promise.resolve({}))
    .then(console.log);
// {glestrade: [ … ], mholmes: [ … ], iadler: [ … ]}

async函數返回一個 Promise 對象,可以使用then方法添加回調函數。當函數執行的時候,一旦遇到await就會先返回,等到異步操作完成,再接着執行函數體內後面的語句。

請注意,在此我們傳遞Promise作爲初始值Promise.resolve(),我們的第一個API調用將立即運行。

下面是不使用async語法糖的版本

function fetchMessages(username) {
    return fetch(`https://example.com/api/messages/${username}`)
        .then(response => response.json());
}

function getUsername(person) {
    return person.username;
}

function chainedFetchMessages(p, username) {
    // In this function, p is a promise. We wait for it to finish,
    // then run fetchMessages().
    return p.then((obj)=>{
        return fetchMessages(username).then(data=>{
            return {
                ...obj,
                [username]: data
            }
        })
    })
}

const msgObj = peopleArr
    .map(getUsername)
    .reduce(chainedFetchMessages, Promise.resolve({}))
    .then(console.log);
// {glestrade: [ … ], mholmes: [ … ], iadler: [ … ]}

PS:更多前端資訊、技術乾貨,請關注公衆號「前端新視界

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章