@alanscodelog/utils
    Preparing search index...

    Function multisplice

    • Splices arrays at multiple positions and returns both the array and the removed elements for easier chaining.

      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

      // remove the first two elements
      multisplice(array, [0, 1], 1)

      //optionally destructure the return to extract removed elements
      const {removed} = multisplice(array, [0, 1], 1)

      // using chaining
      // unlike splice you can pick whether you want the array or the removed elements
      multisplice(array, [0, 1], 1).array
      .filter(() => {...})
      .map(() => {...})

      // splice without mutating
      const {removed, array: splicedArray} = multisplice([...array], [0, 1], 1)

      Deleting more than one item at a time at multiple positions is supported if the ranges created don't overlaps:

      const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

      // deleting 1 item at positions 0, 1 = OK
      // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      // [ ][ ]

      // delete 2 items at positions 0, 1 = ERROR
      // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      // [ ]
      // [ ]

      // delete 10000 items at position 9 = OK (because splice allows it)
      // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      // [...

      You can also insert items. By default they are inserted as they are at each index position passed. Use the insert option if you want to match the items in an array of items to each index position. See MULTISPLICE_ITEM for details.

      The function's types are setup to only take items to insert of the same type that the array already contains, so it will complain depending on what insert option you use. You can always cast the item/s as any and/or pass a different type for the TArray type parameter.

      Type Parameters

      Parameters

      Returns { array: Mutable<TArray>; removed: Mutable<TArray> }