Options
All
  • Public
  • Public/Protected
  • All
Menu

Module Array

JMap.Util.Array

Here you'll find all array related methods

Index

Functions

clear

  • clear<T>(array: T[]): T[]
  • JMap.Util.Array.clear

    Removes all items of the specified array, making it empty.

    example
    
    const myNumbers = [3, 5, 6, 7]
    // remove all items
    JMap.Util.Array.clear(myNumbers)
    console.log(`Count=${myNumbers.length}`)
    // display message "Count=0"

    Type parameters

    • T

    Parameters

    • array: T[]

      the array

    Returns T[]

    the given array (and not a copy)

findByProperty

  • findByProperty<T>(array: T[], propertyName: string, value: any): T | undefined
  • JMap.Util.Array.findByProperty

    Search for first element in the given object array, for a given attribute name and value.

    example
    
    const myObjects = [{
     id: 1,
     name: "Green"
    }, {
     id: 2,
     name: "Red"
    }, {
     id: 3,
     name: "Blue"
    }]
    // search for an object in the given array, having attribute "name" equals to "red", and returns found object or undefined
    const foundObject = JMap.Util.Array.findByProperty(myObjects, "name", "Red")
    console.log(`Found object: "${JSON.parse(foundObject)}"`)
    // display message 'Found object: "{ \"id\": 2, \"name\": \"Red\" }"'

    Type parameters

    • T: object

    Parameters

    • array: T[]

      the array

    • propertyName: string

      the object property name

    • value: any

      object's value for the given property name

    Returns T | undefined

    the found object, undefined if not found

findIndexByProperty

  • findIndexByProperty<T>(array: T[], propertyName: string, value: any, nonStrict?: undefined | false | true): number
  • JMap.Util.Array.findIndexByProperty

    Search for first element index in the given object array, for a given attribute name and value.

    example
    
    const myObjects = [{
     id: 1,
     name: "Green"
    }, {
     id: 2,
     name: "Red"
    }, {
     id: 3,
     name: "Blue"
    }]
    // search for an object in the array that have attribute "name" equals to "red", and return its index position in the array
    const objectIndex = JMap.Util.Array.findIndexByProperty(myObjects, "name", "Red")
    console.log(`Object index: ${objectIndex}`)
    // display message 'Object index: 1'

    Type parameters

    • T: object

    Parameters

    • array: T[]

      the array

    • propertyName: string

      the object property name

    • value: any

      object's value for the given property name

    • Optional nonStrict: undefined | false | true

      by default test the value like "===", if nonStrict is true, will test like "=="

    Returns number

    the found index, -1 if not found

getCopyWithoutDuplicate

  • getCopyWithoutDuplicate(array: Array<string | number>): Array<string | number>
  • JMap.Util.Array.getCopyWithoutDuplicate

    Get a copy of the given array without duplicate.

    example
    
    const myNumbers = [1, 4, 2, 6, 1, 2, 8]
    // returns array copy containing no duplicate
    const copy = JMap.Util.Array.getCopyWithoutDuplicate(myObjects, "name", "Red")
    console.log(`Copy: "${JSON.parse(foundObject)}"`)
    // display message 'Copy: "[1, 4, 2, 6, 8]"'

    Parameters

    • array: Array<string | number>

      the array

    Returns Array<string | number>

    given array copy, without duplicate

remove

  • remove<T>(array: T[], element: T): T[]
  • JMap.Util.Array.remove

    Remove given element in given array.

    example
    
    const myNumbers = [3, 5, 6, 7]
    // remove element 6 in array
    JMap.Util.Array.remove(myNumbers, 6)
    console.log(`Now my numbers are [${myNumbers.join(", ")}]`)
    // display message "Now my numbers are [3, 5, 7]"

    Type parameters

    • T

    Parameters

    • array: T[]

      the array

    • element: T

      the element to remove

    Returns T[]

    the given array (and not a copy)

removeByProperty

  • removeByProperty<T>(array: T[], propertyName: string, value: any, nonStrict?: undefined | false | true): T[]
  • JMap.Util.Array.removeByProperty

    Remove the first occurence.

    example
    
    const myObjects = [{
     id: 1,
     name: "Green"
    }, {
     id: 2,
     name: "Red"
    }, {
     id: 3,
     name: "Blue"
    }]
    // remove in the array the object that have a property "name" equals to "Red"
    JMap.Util.Array.removeByProperty(myObjects, "name", "Red")
    console.log(`myObjects: [${myObjects.join(", "")}]`)
    // display message 'myObjects: [{ \"id\": 1, \"name\": \"Green\" }, { \"id\": 3, \"name\": \"Blue\" }]'

    Type parameters

    • T: object

    Parameters

    • array: T[]

      the array

    • propertyName: string

      the object property name

    • value: any

      object's value for the given property name

    • Optional nonStrict: undefined | false | true

      by default test the value like "===", if nonStrict is true, will test like "=="

    Returns T[]

    the given array (and not a copy)