Function toggleInPlace

  • Type Parameters

    • T extends {}

    Parameters

    • list: T[]
    • item: T
    • Optionaloptions: {
          strategy?: "prepend" | "append";
      }
      • Optionalstrategy?: "prepend" | "append"

    Returns T[]

    use xor instead

    If the item matching the condition already exists in the list it will be removed. If it does not it will be added.

    Mutates the array

    const gods = ['ra', 'zeus', 'loki']
    toggleInPlace(gods, 'ra') // => [zeus, loki]

    const gods = ['zeus', 'loki']
    toggleInPlace(gods, 'ra') // => [zeus, loki, ra]