forEach

forEach

  • (fn: Function) => (collection: Collection) => Collection
  1. Call a function for each entry in collection

    const logEntry = (val, key) => console.log(`${key}: ${val}`)
    const logEach = forEach(logEntry)
    logEach([1, 2, 3])
    // is
    // 0: 1
    // 1: 2
    // 2: 3
    
    logEach({ a: 1, b: 2, c: 3 })
    // is
    // a: 1
    // b: 2
    // c: 3
    const logEntry = (val: unknown, key: unknown) => console.log(`${key}: ${val}`)
    const logEach = forEach(logEntry)
    logEach([1, 2, 3])
    // is
    // 0: 1
    // 1: 2
    // 2: 3
    
    logEach({ a: 1, b: 2, c: 3 })
    // is
    // a: 1
    // b: 2
    // c: 3
    
    const logEntry = (val, key) =>
      console.log(`${key}: ${val}`)
    const logEach = forEach(logEntry)
    logEach([1, 2, 3])
    // is
    // 0: 1
    // 1: 2
    // 2: 3
    
    logEach({ a: 1, b: 2, c: 3 })
    // is
    // a: 1
    // b: 2
    // c: 3
    const logEntry = (val: unknown, key: unknown) =>
      console.log(`${key}: ${val}`)
    const logEach = forEach(logEntry)
    logEach([1, 2, 3])
    // is
    // 0: 1
    // 1: 2
    // 2: 3
    
    logEach({ a: 1, b: 2, c: 3 })
    // is
    // a: 1
    // b: 2
    // c: 3
    
  2. Sometimes we need to call a function for each item in a collection. Below, we are running a social media site. We track when people appear in search results to monitor trends. Let's update our database with two recent searches.

    const searchResults1 = ['chris', 'liz', 'phil']
    const searchResults2 = ['chris', 'liz']
    
    const db = {
      searchResultsByName: {},
    }
    
    const incrementSearchResult = name => {
      if (db.searchResultsByName[name] === undefined) {
        db.searchResultsByName[name] = 0
      }
      db.searchResultsByName[name] += 1
    }
    
    const incrementResults = forEach(incrementSearchResult)
    
    incrementResults(searchResults1)
    incrementResults(searchResults2)
    
    console.log(db.searchResultsByName)
    // is {
    //   chris: 2
    //   liz: 2
    //   phil: 1
    // }
    const searchResults1 = ['chris', 'liz', 'phil']
    const searchResults2 = ['chris', 'liz']
    
    const db = {
      searchResultsByName: {} as Record<string, number | undefined>,
    }
    
    const incrementSearchResult = (name: string) => {
      if (db.searchResultsByName[name] === undefined) {
        db.searchResultsByName[name] = 0
      }
      db.searchResultsByName[name] += 1
    }
    
    const incrementResults = forEach(incrementSearchResult)<string[]>
    
    incrementResults(searchResults1)
    incrementResults(searchResults2)
    
    console.log(db.searchResultsByName)
    // is {
    //   chris: 2
    //   liz: 2
    //   phil: 1
    // }
    
    const searchResults1 = ['chris', 'liz', 'phil']
    const searchResults2 = ['chris', 'liz']
    
    const db = {
      searchResultsByName: {},
    }
    
    const incrementSearchResult = name => {
      if (
        db.searchResultsByName[name] === undefined
      ) {
        db.searchResultsByName[name] = 0
      }
      db.searchResultsByName[name] += 1
    }
    
    const incrementResults = forEach(
      incrementSearchResult
    )
    
    incrementResults(searchResults1)
    incrementResults(searchResults2)
    
    console.log(db.searchResultsByName)
    // is {
    //   chris: 2
    //   liz: 2
    //   phil: 1
    // }
    const searchResults1 = ['chris', 'liz', 'phil']
    const searchResults2 = ['chris', 'liz']
    
    const db = {
      searchResultsByName: {} as Record<
        string,
        number | undefined
      >,
    }
    
    const incrementSearchResult = (name: string) => {
      if (
        db.searchResultsByName[name] === undefined
      ) {
        db.searchResultsByName[name] = 0
      }
      db.searchResultsByName[name] += 1
    }
    
    const incrementResults = forEach(
      incrementSearchResult
    )<string[]>
    
    incrementResults(searchResults1)
    incrementResults(searchResults2)
    
    console.log(db.searchResultsByName)
    // is {
    //   chris: 2
    //   liz: 2
    //   phil: 1
    // }
    
  3. forEach is intended for side effects. If you're using this to modify the collection then you're likely misunderstanding Common FP. Please raise an issue so we can figure out a better approach for ya.