sumValues

sumValues

  • (collection: Numbers) => number
  1. Return the sum of a collection's values

    sumValues([1, 3]) // is 4
    sumValues([1, 3]) // is 4
    
    sumValues([1, 3]) // is 4
    sumValues([1, 3]) // is 4
    
  2. We often want to total up some numbers. Below, Jen and Mike went out to breakfast. Let's figure their subtotal.

    const order = {
      jen: {
        'eggs and more': 9.5,
        coffee: 2,
      },
      mike: {
        'chicken and waffles': 11,
        espresso: 3,
      },
    }
    
    const getSubtotal = compose([mapValues(sumValues), sumValues])
    
    const subtotal = getSubtotal(order)
    console.log(subtotal) // is 25.50
    type Order = Record<string, Record<string, number>>
    const order: Order = {
      jen: {
        'eggs and more': 9.5,
        coffee: 2,
      },
      mike: {
        'chicken and waffles': 11,
        espresso: 3,
      },
    }
    
    const getSubtotal = compose([mapValues(sumValues)<Order>, sumValues])
    
    const subtotal = getSubtotal(order)
    console.log(subtotal) // is 25.5
    
    const order = {
      jen: {
        'eggs and more': 9.5,
        coffee: 2,
      },
      mike: {
        'chicken and waffles': 11,
        espresso: 3,
      },
    }
    
    const getSubtotal = compose([
      mapValues(sumValues),
      sumValues,
    ])
    
    const subtotal = getSubtotal(order)
    console.log(subtotal) // is 25.50
    type Order = Record<
      string,
      Record<string, number>
    >
    const order: Order = {
      jen: {
        'eggs and more': 9.5,
        coffee: 2,
      },
      mike: {
        'chicken and waffles': 11,
        espresso: 3,
      },
    }
    
    const getSubtotal = compose([
      mapValues(sumValues)<Order>,
      sumValues,
    ])
    
    const subtotal = getSubtotal(order)
    console.log(subtotal) // is 25.5