Bitbybit Docs
    Preparing search index...

    Class Lists

    Reading, building and reshaping plain arrays of any kind of item. Positions are 0-based: index 0 is the first item. Most methods take a clone option, on by default, that deep-copies the list first so the input is never changed; switched off, the modifying methods work on the list in place, which is faster for large data. removeAllItems always empties the list it is given.

    Index

    Constructors

    add

    • Inserts an item at a position; the items from that position on shift up by one.

      Example: [10, 20, 30, 40] with 99 at index 2 -> [10, 20, 99, 30, 40]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list with the item inserted

      const longer = bitbybit.lists.addItemAtIndex({ list: [10, 20, 30, 40], item: 99, index: 2, clone: true });
      
    • Inserts the same item at several positions of the original list.

      Example: [10, 20, 30] with 99 at indexes [0, 2] -> [99, 10, 20, 99, 30]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list with the item inserted at each index

      const marked = bitbybit.lists.addItemAtIndexes({ list: [10, 20, 30], item: 99, indexes: [0, 2], clone: true });
      
    • Inserts several items, the first at the first index, the second at the second, and so on, all counted on the original list.

      The indexes must be in ascending order and there must be one per item, or an error is thrown. Example: [10, 20, 30] with items [88, 99] at indexes [1, 2] -> [10, 88, 20, 99, 30]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list with the items inserted

      const merged = bitbybit.lists.addItemsAtIndexes({ list: [10, 20, 30], items: [88, 99], indexes: [1, 2], clone: true });
      
    • Adds an item at the end of the list.

      Example: [10, 20, 30] adding 40 -> [10, 20, 30, 40]

      Type Parameters

      • T

      Parameters

      • inputs: AddItemDto<T>

        The list, the item and whether to copy

      Returns T[]

      The list with the item at its end

      const longer = bitbybit.lists.addItem({ list: [10, 20, 30], item: 40, clone: true });
      
    • Adds an item at the start of the list.

      Example: [10, 20, 30] prepending 5 -> [5, 10, 20, 30]

      Type Parameters

      • T

      Parameters

      • inputs: AddItemDto<T>

        The list, the item and whether to copy

      Returns T[]

      The list with the item at its start

      const longer = bitbybit.lists.prependItem({ list: [10, 20, 30], item: 5, clone: true });
      
    • Adds an item at the start or at the end of the list, as chosen.

      Example: [10, 20, 30] adding 5 first -> [5, 10, 20, 30]; last -> [10, 20, 30, 5]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list with the item added

      const longer = bitbybit.lists.addItemFirstLast({ list: [10, 20, 30], item: 5, position: Bit.Inputs.Lists.firstLastEnum.first, clone: true });
      
    • Joins several lists into one, end to end.

      Example: [[1, 2], [3, 4], [5, 6]] -> [1, 2, 3, 4, 5, 6]

      Type Parameters

      • T

      Parameters

      Returns T[]

      One list with all the items

      const all = bitbybit.lists.concatenate({ lists: [[1, 2], [3, 4], [5, 6]], clone: true });
      

    create

    • Makes a new list with nothing in it.

      Example: -> []

      Returns []

      An empty list

    • Makes a list that holds the same item a number of times.

      Example: 5 three times -> [5, 5, 5]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list of repeats

      const fives = bitbybit.lists.repeat({ item: 5, times: 3 });
      
    • Repeats a pattern of items over and over until the list reaches a given length.

      Example: [1, 2, 3] to length 7 -> [1, 2, 3, 1, 2, 3, 1]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The repeated pattern, cut to the length

      const cycle = bitbybit.lists.repeatInPattern({ list: [1, 2, 3], lengthLimit: 7, clone: true });
      

    edit

    • Reverses the order of the items.

      Example: [1, 2, 3, 4, 5] -> [5, 4, 3, 2, 1]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The reversed list

    • Puts the items in a random order, different on every call.

      Example: [1, 2, 3, 4, 5] -> perhaps [3, 1, 5, 2, 4]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The shuffled list

    • Turns a list of lists on its side: rows become columns and columns become rows.

      All the inner lists must have the same length. Example: [[0, 1, 2], [3, 4, 5]] -> [[0, 3], [1, 4], [2, 5]]

      Type Parameters

      • T

      Parameters

      • inputs: ListCloneDto<T[]>

        The list of lists and whether to copy it first

      Returns T[][]

      The transposed list of lists

      const columns = bitbybit.lists.flipLists({ list: [[0, 1, 2], [3, 4, 5]], clone: true });
      
    • Splits the list into groups of n items.

      Items left over at the end are dropped unless keepRemainder is on, which adds them as a shorter last group. Example: [0, 1, 2, 3, 4, 5, 6, 7, 8] in groups of 3 -> [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

      Type Parameters

      • T

      Parameters

      • inputs: GroupListDto<T>

        The list, the group size and whether to keep a partial last group

      Returns T[][]

      The groups, in order

      const pairs = bitbybit.lists.groupNth({ list: [0, 1, 2, 3, 4], nrElements: 2, keepRemainder: true });
      

    get

    • Reads the item at a position in the list, counting from 0.

      An index outside the list throws an error. Example: [10, 20, 30, 40] at index 2 -> 30

      Type Parameters

      • T

      Parameters

      • inputs: ListItemDto<T>

        The list, the index and whether to copy the item

      Returns T

      The item at that index

      const third = bitbybit.lists.getItem({ list: [10, 20, 30, 40], index: 2, clone: true });
      
    • Reads the first item of the list.

      Example: [10, 20, 30, 40] -> 10

      Type Parameters

      • T

      Parameters

      Returns T

      The first item

    • Reads the last item of the list.

      Example: [10, 20, 30, 40] -> 40

      Type Parameters

      • T

      Parameters

      Returns T

      The last item

    • Keeps each item of the list with a given probability and drops the rest, so the result differs on every call.

      Example: [1, 2, 3, 4, 5] with threshold 0.5 -> perhaps [1, 3, 5]

      Type Parameters

      • T

      Parameters

      • inputs: RandomThresholdDto<T>

        The list, the probability of keeping an item from 0 to 1, and whether to copy

      Returns T[]

      The items that were kept, in their original order

      const some = bitbybit.lists.randomGetThreshold({ list: [1, 2, 3, 4, 5], threshold: 0.5, clone: true });
      
    • Cuts out the items from a start index up to, but not including, an end index.

      Example: [10, 20, 30, 40, 50] from 1 to 4 -> [20, 30, 40]

      Type Parameters

      • T

      Parameters

      • inputs: SubListDto<T>

        The list, the start and end indexes, and whether to copy

      Returns T[]

      The items in that range

      const middle = bitbybit.lists.getSubList({ list: [10, 20, 30, 40, 50], indexStart: 1, indexEnd: 4, clone: true });
      
    • Keeps every nth item, starting from an offset.

      Example: [0, 1, 2, 3, 4, 5, 6, 7, 8] with nth 3 and offset 0 -> [0, 3, 6]; with nth 2 and offset 1 -> [1, 3, 5, 7]

      Type Parameters

      • T

      Parameters

      • inputs: GetNthItemDto<T>

        The list, the step, the offset to start from, and whether to copy

      Returns T[]

      Every nth item, in order

      const everyThird = bitbybit.lists.getNthItem({ list: [0, 1, 2, 3, 4, 5, 6, 7, 8], nth: 3, offset: 0, clone: true });
      
    • Keeps the items where a repeating true/false pattern says true and drops the others.

      The pattern starts over when it runs out. Example: [0, 1, 2, 3, 4, 5] with pattern [true, true, false] -> [0, 1, 3, 4]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The items the pattern kept, in order

      const kept = bitbybit.lists.getByPattern({ list: [0, 1, 2, 3, 4, 5], pattern: [true, true, false] });
      
    • Regroups nested lists by position: the first items of every list go together, then the second items, and so on.

      level says how many levels of nesting to flatten inside each list first; 0 regroups them as they are. Example: [[0, 1, 2], [3, 4, 5]] at level 0 -> [[0, 3], [1, 4], [2, 5]]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The regrouped lists

      const columns = bitbybit.lists.mergeElementsOfLists({ lists: [[0, 1, 2], [3, 4, 5]], level: 0 });
      
    • Measures the longest list among several.

      Example: [[1, 2], [3, 4, 5, 6], [7]] -> 4

      Type Parameters

      • T

      Parameters

      Returns number

      The length of the longest one

      const longest = bitbybit.lists.getLongestListLength({ lists: [[1, 2], [3, 4, 5, 6], [7]] });
      
    • Tells whether an item is in the list.

      Items are compared by identity, so an object is found only if the very same object is in the list. Example: [10, 20, 30, 40] includes 30 -> true, includes 50 -> false

      Type Parameters

      • T

      Parameters

      Returns boolean

      True when the item is in the list

      const found = bitbybit.lists.includes({ list: [10, 20, 30, 40], item: 30 });
      
    • Finds the position of the first occurrence of an item, or -1 when it is not in the list.

      Example: [10, 20, 30, 20, 40] finding 20 -> 1, finding 50 -> -1

      Type Parameters

      • T

      Parameters

      Returns number

      The 0-based index, or -1

      const where = bitbybit.lists.findIndex({ list: [10, 20, 30, 20, 40], item: 20 });
      
    • Measures how deeply lists are nested inside the list.

      Example: [1, 2, 3] -> 1, [[1, 2], [3, 4]] -> 2, [[[1]]] -> 3

      Parameters

      Returns number

      The number of nesting levels

    • Counts the items in the list.

      Example: [10, 20, 30, 40, 50] -> 5, [] -> 0

      Type Parameters

      • T

      Parameters

      Returns number

      The number of items

    remove

    • Removes the item at a position.

      Example: [10, 20, 30, 40, 50] removing index 2 -> [10, 20, 40, 50]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list without that item

      const shorter = bitbybit.lists.removeItemAtIndex({ list: [10, 20, 30, 40, 50], index: 2, clone: true });
      
    • Removes the first item.

      Example: [10, 20, 30, 40] -> [20, 30, 40]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list without its first item

    • Removes the last item.

      Example: [10, 20, 30, 40] -> [10, 20, 30]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list without its last item

    • Removes an item counted from the end: index 0 is the last item, 1 the one before it.

      Example: [10, 20, 30, 40, 50] removing index 1 from the end -> [10, 20, 30, 50]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list without that item

      const shorter = bitbybit.lists.removeItemAtIndexFromEnd({ list: [10, 20, 30, 40, 50], index: 1, clone: true });
      
    • Removes the items at several positions, all counted on the original list.

      Example: [10, 20, 30, 40, 50] removing indexes [1, 3] -> [10, 30, 50]

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list without those items

      const kept = bitbybit.lists.removeItemsAtIndexes({ list: [10, 20, 30, 40, 50], indexes: [1, 3], clone: true });
      
    • Empties the list it is given, in place: the same array comes back with no items in it.

      Example: [10, 20, 30, 40] -> []

      Type Parameters

      • T

      Parameters

      Returns T[]

      The same list, now empty

    • Removes every nth item, starting from an offset.

      Example: [0, 1, 2, 3, 4, 5, 6, 7, 8] with nth 3 and offset 0 -> [1, 2, 4, 5, 7, 8]

      Type Parameters

      • T

      Parameters

      • inputs: RemoveNthItemDto<T>

        The list, the step, the offset to start from and whether to copy

      Returns T[]

      The list without every nth item

      const thinned = bitbybit.lists.removeNthItem({ list: [0, 1, 2, 3, 4, 5, 6, 7, 8], nth: 3, offset: 0, clone: true });
      
    • Drops each item of the list with a given probability and keeps the rest, so the result differs on every call.

      Example: [1, 2, 3, 4, 5] with threshold 0.5 -> perhaps [2, 4]

      Type Parameters

      • T

      Parameters

      • inputs: RandomThresholdDto<T>

        The list, the probability of dropping an item from 0 to 1, and whether to copy

      Returns T[]

      The items that survived, in their original order

      const some = bitbybit.lists.randomRemoveThreshold({ list: [1, 2, 3, 4, 5], threshold: 0.5, clone: true });
      
    • Removes repeated numbers, keeping the first occurrence of each.

      Example: [1, 2, 3, 2, 4, 3, 5] -> [1, 2, 3, 4, 5]

      Parameters

      Returns number[]

      The numbers without repeats, in their original order

    • Removes numbers that are within a tolerance of one already kept, so values that differ only by floating-point noise count as the same.

      Example: [1.0, 1.001, 2.0, 2.002, 3.0] with tolerance 0.01 -> [1.0, 2.0, 3.0]

      Parameters

      Returns number[]

      The numbers without near-repeats, in their original order

      const distinct = bitbybit.lists.removeDuplicateNumbersTolerance({ list: [1.0, 1.001, 2.0], tolerance: 0.01, clone: true });
      
    • Removes repeated items of any kind, keeping the first occurrence of each.

      Items are compared by identity, so two equal-looking objects both stay. Example: ['a', 'b', 'c', 'a', 'd', 'b'] -> ['a', 'b', 'c', 'd']

      Type Parameters

      • T

      Parameters

      Returns T[]

      The list without repeats, in its original order

    sorting

    • Sorts numbers from lowest to highest, or from highest to lowest.

      Example: [5, 2, 8, 1, 9] ascending -> [1, 2, 5, 8, 9]; descending -> [9, 8, 5, 2, 1]

      Parameters

      • inputs: SortDto<number>

        The numbers, the direction and whether to copy

      Returns number[]

      The sorted numbers

      const sorted = bitbybit.lists.sortNumber({ list: [5, 2, 8, 1, 9], orderAsc: true, clone: true });
      
    • Sorts texts alphabetically, from A to Z or from Z to A.

      Example: ['dog', 'apple', 'cat'] ascending -> ['apple', 'cat', 'dog']

      Parameters

      • inputs: SortDto<string>

        The texts, the direction and whether to copy

      Returns string[]

      The sorted texts

      const sorted = bitbybit.lists.sortTexts({ list: ["dog", "apple", "cat"], orderAsc: true, clone: true });
      
    • Sorts objects by the number held in one of their properties.

      Example: [{age: 30}, {age: 20}, {age: 25}] by 'age' ascending -> [{age: 20}, {age: 25}, {age: 30}]

      Parameters

      • inputs: SortJsonDto<any>

        The objects, the property to sort by, the direction and whether to copy

      Returns any[]

      The sorted objects

      const byAge = bitbybit.lists.sortByPropValue({ list: [{ age: 30 }, { age: 20 }], property: "age", orderAsc: true, clone: true });
      

    transform

    • Weaves several lists into one by taking the first item of each in turn, then the second of each, and so on.

      A shorter list simply drops out once it runs dry. An empty list of lists throws an error. Example: [[0, 1, 2], [3, 4, 5]] -> [0, 3, 1, 4, 2, 5]

      Type Parameters

      • T

      Parameters

      • inputs: InterleaveDto<T>

        The lists to weave together and whether to copy

      Returns T[]

      One list with the items alternating

      const woven = bitbybit.lists.interleave({ lists: [[0, 1, 2], [3, 4, 5]], clone: true });