pols-utils - v5.3.11
    Preparing search index...

    Function toggleElement

    • Inserts an element into an array if the check callback function returns false for every element. Otherwise, it removes the element(s) for wich check returns true.

      Type Parameters

      • T

      Parameters

      • array: T[]

        The target array where the element will be toggled.

      • element: T

        The element to insert if check returns false for all elements.

      • check: (element: T, index?: number) => boolean

        A callback function to check each element in the array.

      Returns void

      The modified array with the element toggled (added or removed).

      const pets = [
      { type: 'dog', name: 'Fido' },
      { type: 'dog', name: 'Chacalin' },
      { type: 'cat', name: 'Mr. Brown' },
      { type: 'cat', name: 'Night' },
      ]
      PUtilsArray.toggleElement(pets, { type: 'dog', name: 'Chacalin' }, pet => pet.name == 'Chacalin')
      console.log(pets)
      // [
      // { type: 'dog', name: 'Fido' },
      // { type: 'cat', name: 'Mr. Brown' },
      // { type: 'cat', name: 'Night' },
      // ]
      PUtilsArray.toggleElement(pets, { type: 'dog', name: 'Chacalin' }, pet => pet.name == 'Chacalin')
      console.log(pets)
      // [
      // { type: 'dog', name: 'Fido' },
      // { type: 'cat', name: 'Mr. Brown' },
      // { type: 'cat', name: 'Night' },
      // { type: 'dog', name: 'Chacalin' },
      // ]