Search code examples
javascriptlodash

JS & Lodash array find and delete methods


Say I have an array in js:

let arr = ['one', 'two', 'three', 'four']
  1. How would I search through the array and check if the 'three' element exists in the array and return true/false.

  2. How would I delete the a given element, (ex. 'two') from the array.

Are there lodash methods for this?


Solution

  • You don't need lodash:

    arr.includes("three") // true
    arr.includes("five") // false
    
    // the 1 means to delete one element
    arr.splice(arr.indexOf("two"), 1)
    arr // ["one", "three", "four"]