Options
All
  • Public
  • Public/Protected
  • All
Menu

array-simple-query

Index

1-RETRIEVE Functions

  • filterObjects(arrObj: any[], query: any, firstMatch?: boolean): any[]
  • This method returns a new array composed of all elements that match the query. It accepts nested queries. In case no match is found, it returns an empty array.

    see

    getObject getObjectbyIds

    example
    const books = [
    {'id':1,'title':'English course','author':{first_name:'Joe',last_name:'Doe'},'year':2009},
    {'id':2,'title':'Italian course','author':{first_name:'Pico',last_name:'Pallino'},'year':2010},
    {'id':3,'title':'German course','author':{first_name:'Max',last_name:'Musterman'},'year':2009}
    ]

    const selectedBooks = filterObjects(books,{'!title': 'German course'});

    console.log(selectedBooks)
    // => [{ 'id': 1, 'title': 'English course', 'author': {first_name: 'Joe', last_name:'Doe'} },
    // { 'id': 2, 'title': 'Italian course', 'author': {first_name: ' Pico', last_name:'Pallino'} }]'

    Parameters

    • arrObj: any[]

      An array of objects.

    • query: any

      A key-value object representing the query. Use "." for nested queries ({"parent.child": "value"}) and "!" for negation ({"!field": "value"}).

    • firstMatch: boolean = false

    Returns any[]

    Returns an array of matched object.

  • getObject(arrObj: any[], query: any): any
  • This method retrieves the first object that matches the input query. It accepts nested queries. It returns the original object. In case no match is found, it returns null.

    see

    filterObjects

    example
    const books = [
    {'id':1,'title':'English course','author':{first_name:'Joe',last_name:'Doe'},'year':2009},
    {'id':2,'title':'Italian course','author':{first_name:'Pico',last_name:'Pallino'},'year':2010},
    {'id':3,'title':'German course','author':{first_name:'Max',last_name:'Musterman'},'year':2009}
    ]

    const englishBook = getObject(books, {'title':'English course'});
    const italianBook = getObject(books, {'!title':'English course'});
    const pallinosBook = getObject(books, {'author.last_name':'Pallino'});

    Parameters

    • arrObj: any[]

      An array of objects.

    • query: any

      A key-value object representing the query. Use "." for nested queries ({"parent.child": "value"}) and "!" for negation ({"!field": "value"})..

    Returns any

    Returns the matched object.

  • getObjectbyIds(arrObj: any[], listIds: number[]): any[]
  • This method returns a new object array composed of elements whose id numbers are found in a given list. In case no match is found, it returns an empty array.

    see

    getObject

    example
    const books = [
    {'id':1,'title':'English course','author':{first_name:'Joe',last_name:'Doe'},'year':2009},
    {'id':2,'title':'Italian course','author':{first_name:'Pico',last_name:'Pallino'},'year':2010},
    {'id':3,'title':'German course','author':{first_name:'Max',last_name:'Musterman'},'year':2009}
    ]

    const selectedBooks = getObject(books,[1,2]);
    console.log(selectedBooks)
    // => [{ 'id': 1, 'title': 'English course', 'author': {first_name: 'Joe', last_name:'Doe'} },
    // { 'id': 2, 'title': 'Italian course', 'author': {first_name: ' Pico', last_name:'Pallino'} }]'

    Parameters

    • arrObj: any[]

      An array of objects.

    • listIds: number[]

      An array of the selected id numbers.

    Returns any[]

    Returns an array of matched ids.

2-DELETE Functions

  • deleteByArrayIndexes(arrObj: any[], indexes: number[]): void
  • This method modifies the input array by deleting all elements in the positions given by list of indexes.

    see

    deleteById

    example
    const books = [
    {'id':1,'title':'English course','author':{first_name:'Joe',last_name:'Doe'},'year':2009},
    {'id':2,'title':'Italian course','author':{first_name:'Pico',last_name:'Pallino'},'year':2010},
    {'id':3,'title':'German course','author':{first_name:'Max',last_name:'Musterman'},'year':2009}
    ]

    deleteByArrayIndexes(books, [1, 2])

    console.log(books)
    // => [ {'id': 1, 'title': 'English course', 'author': {first_name: 'Joe', last_name:'Doe'}, 'year': 2009}]

    Parameters

    • arrObj: any[]

      An array of objects.

    • indexes: number[]

      array indexes to be deleted.

    Returns void

    Returns undefined.

  • deleteById(arrObj: any[], id: string | number): void
  • This method modifies the input array by deleting the element with given id.

    see

    deleteByIdInNewArray deleteObjects

    example
    const books = [
    {'id':1,'title':'English course','author':{first_name:'Joe',last_name:'Doe'},'year':2009},
    {'id':2,'title':'Italian course','author':{first_name:'Pico',last_name:'Pallino'},'year':2010},
    {'id':3,'title':'German course','author':{first_name:'Max',last_name:'Musterman'},'year':2009}
    ]


    deleteById(books, 2);

    console.log(books)
    // => [{ 'id': 1, 'title': 'English course', 'author': {first_name: 'Joe', last_name:'Doe'} },
    // { 'id': 3, 'title': 'German course', 'author': {first_name: 'Max', last_name:'Musterman'} }],

    Parameters

    • arrObj: any[]

      An array of objects.

    • id: string | number

      Unique string or number that identifies the object.

    Returns void

    Returns undefined.

  • deleteByIdInNewArray(arrObj: any[], id: string | number): any[]
  • This method creates a shallow copy of the input array and remove the element of given id.

    see

    deleteById deleteObjects

    example
    const books = [
    {'id':1,'title':'English course','author':{first_name:'Joe',last_name:'Doe'},'year':2009},
    {'id':2,'title':'Italian course','author':{first_name:'Pico',last_name:'Pallino'},'year':2010},
    {'id':3,'title':'German course','author':{first_name:'Max',last_name:'Musterman'},'year':2009}
    ]


    const newBookArray = deleteByIdInNewArray(books, 2);

    console.log(books)
    // => [{ 'id': 1, 'title': 'English course', 'author': {first_name: 'Joe', last_name:'Doe'} },
    // { 'id': 2, 'title': 'Italian course', 'author': {first_name: ' Pico', last_name:'Pallino'} },
    // { 'id': 3, 'title': 'German course', 'author': {first_name: 'Max', last_name:'Musterman'} }],

    console.log(newBookArray)
    // => [{ 'id': 1, 'title': 'English course', 'author': {first_name: 'Joe', last_name:'Doe'} },
    // { 'id': 3, 'title': 'German course', 'author': {first_name: 'Max', last_name:'Musterman'} }],

    Parameters

    • arrObj: any[]

      An array of objects.

    • id: string | number

      Unique string or number that identifies the object.

    Returns any[]

    Returns new array with element removed.

  • deleteObjects(arrObj: any[], query: any): number[]
  • This method modifies the input array by deleting all elements that match the query.

    see

    deleteById

    see

    deleteByIdInNewArray

    example
    const books = [
    {'id':1,'title':'English course','author':{first_name:'Joe',last_name:'Doe'},'year':2009},
    {'id':2,'title':'Italian course','author':{first_name:'Pico',last_name:'Pallino'},'year':2010},
    {'id':3,'title':'German course','author':{first_name:'Max',last_name:'Musterman'},'year':2009}
    ]

    deletedArrayPos = deleteObjects(books, {'year': 2009});

    console.log(books)
    // => [ { 'id': 2, 'title': 'Italian course', 'author': {first_name: ' Pico', last_name:'Pallino'}, 'year': 2010 }]

    console.log(deletedArrayPos)
    // => [0, 2]

    Parameters

    • arrObj: any[]

      An array of objects.

    • query: any

      A key-value object representing the query. Use "." for nested queries ({"parent.child": "value"}) and "!" for negation ({"!field": "value"}).

    Returns number[]

    Returns the array indexes of the removed object.

3-UPDATE Functions

  • updateObject(arrObj: [any], id: string | number, patch: any): boolean
  • This method updates an element chosen by its id inside the input array. Each object must have an 'id' key. It returns 'true' if the update is successfull.

    see

    getObject getObjectbyIds

    example
    const books = [
    {'id':1,'title':'English course','author':{first_name:'Joe',last_name:'Doe'},'year':2009},
    {'id':2,'title':'Italian course','author':{first_name:'Pico',last_name:'Pallino'},'year':2010},
    {'id':3,'title':'German course','author':{first_name:'Max',last_name:'Musterman'},'year':2009}
    ]


    updateObject(books, 2, {title: 'Corso di Italiano'});

    console.log(books)
    // => [{ 'id': 1, 'title': 'English course', 'author': {first_name: 'Joe', last_name:'Doe'} },
    // { 'id': 2, 'title': 'Corso di Italiano', 'author': {first_name: ' Pico', last_name:'Pallino'} },
    // { 'id': 3, 'title': 'German course', 'author': {first_name: 'Max', last_name:'Musterman'} }],

    Parameters

    • arrObj: [any]

      An array of objects.

    • id: string | number

      Unique string or number that identifies the object.

    • patch: any

      a partial object containing the fields and values to be updated.

    Returns boolean

    Returns an array of matched object.

4-OTHERS Functions

  • extractIds(originalData: any): any
  • This method returns the entity id number.

    Parameters

    • originalData: any

    Returns any

  • extractNestedIds(object: any, field: any): any
  • This method reduces a nested field (db relationship) to its id or array of ids.

    Parameters

    • object: any
    • field: any

    Returns any

Generated using TypeDoc