Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "array/replace"

Index

Functions

replace

  • replace<T>(array: T[], itemToRemove: T, itemToReplace: T): T[]
  • replace<T>(array: T[], replaceOptions: ReplaceItemsOptions<T>): T[]
  • replace<T>(array: T[], replaceOptions: ReplaceByMapOptions<T>): T[]
  • Replaces an item with passed one of an array


    Example:

    import { replace } from "@thalesrc/js-utils/array";
    
    const array = ["a", "b", "c", "a", "b", "c"];
    
    replace(array, "b", "x"); // ["a", "x", "c", "a", "b", "c"]

    Array Prototype Example:

    import "@thalesrc/js-utils/array/proto/replace";
    
    const array = ["a", "b", "c", "a", "b", "c"];
    
    array.replace("b", "x"); // ["a", "x", "c", "a", "b", "c"]

    template

    T Typeof array items

    Type parameters

    • T

    Parameters

    • array: T[]

      Array to replace its item

    • itemToRemove: T

      Item to remove

    • itemToReplace: T

      Item to replace with

    Returns T[]

    New replaced array

  • Deletes items and replaces new ones from passed starting index of an array


    Example:

    import { replace } from "@thalesrc/js-utils";
    
    const array = ["a", "b", "c", "a", "b", "c"];
    
    replace(array, {startingIndex: 3, deleteCount: 1, itemsToReplace: ['x', 'y']}); // ["a", "b", "c", "x", "y", "b", "c"];

    Array Prototype Example:

    import "@thalesrc/js-utils/dist/as-proto/array-replace";
    
    const array = ["a", "b", "c", "a", "b", "c"];
    
    array.replace({startingIndex: 3, deleteCount: 1, itemsToReplace: ['x', 'y']}); // ["a", "b", "c", "x", "y", "b", "c"];

    template

    T Typeof array items

    Type parameters

    • T

    Parameters

    Returns T[]

    New replaced array

  • Deletes items and replaces new ones by passed matcher map


    Example:

    import { replace } from "@thalesrc/js-utils";
    
    const array = ["a", "b", "c", "a", "b", "c"];
    const map = new Map();
    map.set("a", "x")
    map.set("b", "y");
    
    replace(array, {itemsToReplace: map}); // ["x", "y", "c", "a", "b", "c"];
    replace(array, {itemsToReplace: map, multi: true}); // ["x", "y", "c", "x", "y", "c"];

    Array Prototype Example:

    import "@thalesrc/js-utils/dist/as-proto/array-replace";
    
    const array = ["a", "b", "c", "a", "b", "c"];
    const map = new Map();
    map.set("a", "x")
    map.set("b", "y");
    
    array.replace({itemsToReplace: map}); // ["x", "y", "c", "a", "b", "c"];
    array.replace({itemsToReplace: map, multi: true}); // ["x", "y", "c", "x", "y", "c"];

    template

    T Typeof array items

    Type parameters

    • T

    Parameters

    Returns T[]

    New replaced array

Generated using TypeDoc