Search code examples
javascriptecmascript-6lodash

How to write lodash function in es6 style?


I have old lodash function:

  data = _.reject(items, function(i) {
    return i.id == id;
  })

how to write it in es6 way?

  data = _.reject(items, i => {
    return i.id == id;
  })

got error


Solution

  • You could just not use lodash in this case, depending on what your question actually is:

    const items = [2, 3, 88, 99]
    
    const smallItems = items.filter(item => item < 50)
    
    console.log(smallItems) // 2, 3 // High numbers are rejected
    

    Or... you could write up your own reject in ES6 depending on your needs

    // Create function once 
    const reject = (data, predicate) => data.filter(item => !predicate(item))
    
    // Use elsewhere
    const items = [2, 3, 88, 99]
    const smallItems = reject(items, item => item > 50)    
    console.log(smallItems)