Leverege Platform

Leverege Platform

  • Overview
  • JSDocs
  • REST API
  • Help

›Utilities

Platform Overview

  • Overview
  • Architecture
  • Core Concepts

Guides

  • Platform Setup
  • Building a Project
  • API UI

Services

  • API Server
  • Message Processor
  • Transponder
  • Reason
  • Scheduler
  • Messenger
  • Emailer
  • Rest Server
  • DB Curator

API

  • API
  • Interface
  • Project
  • System
  • Device
  • Blueprint
  • Collection
  • Child
  • History
  • Scenario
  • Script
  • Template
  • Timer
  • User
  • Users

Server Libraries

  • Log
  • Messages
  • Message Queue
  • Cache
  • Lock
  • Limit
  • Cluster Manager
  • Error
  • Exit

Utilities

  • Promise
  • Data Store
  • Factory
  • Path
  • Reasoner
  • Object Util
  • Array Util
  • String Util
  • Base62 Util
  • Value Cache
  • Validator
  • Lol

UI

  • API Redux
  • UI Builder
  • UI Elements
  • UI Redux
  • UI Mapbox Elements
  • UI Active Theme
  • UI Color Elements
  • UI Linear View Elements

Array Util

This library contains methods that expand on the functionality of common array methods

Install

npm install --save @leverege/array-util

const ArrayUtil = require( '@leverege/array-util' );

index( array, key, value, from = 0 )

This will look for the first item whose key == value and return its index. If an item is not found, -1 is returned.

const arr = [ { id : 1, name : 'hamster' }, { id : 2, name : 'cat' }, { id : 3, name : 'dog' } ]

ArrayUtil.index( arr, 'name', 'cat' )       // 1
ArrayUtil.index( arr, 'name', 'cat', 1 )    // 1
ArrayUtil.index( arr, 'name', 'cat', 2 )    // -1

find( array, key, value, from = 0 )

This will look for the first item whose key == value and return it. If one is not found, null is returned.

const arr = [ { id : 1, name : 'hamster' }, { id : 2, name : 'cat' }, { id : 3, name : 'dog' } ]

ArrayUtil.find( arr, 'name', 'cat' )       // { id: 2, name: 'cat' }
ArrayUtil.find( arr, 'name', 'cat', 1 )    // { id: 2, name: 'cat' }
ArrayUtil.find( arr, 'name', 'cat', 2 )    // null

replace( array, key, value, elem, from = 0 )

This will look for the first item whose key == value and replace it with elem. The index of the replacement is returned. If one is not found, -1 is returned.

const arr = [ { id : 1, name : 'hamster' }, { id : 2, name : 'cat' }, { id : 3, name : 'dog' } ]

ArrayUtil.replace( arr, 'name', 'cat', { name:'cat', active:false } )      // 1
// arr = [ { id: 1, name: 'hamster' }, { name: 'cat', active: false }, { id: 3, name: 'dog' } ]
ArrayUtil.replace( arr, 'name', 'cat', { name:'cat', active:true }, 1 )    // 1
// arr = [ { id: 1, name: 'hamster' }, { name: 'cat', active: true }, { id: 3, name: 'dog' } ]
ArrayUtil.replace( arr, 'name', 'cat', { name:'cat' }, 2 )                 // -1
// arr = [ { id: 1, name: 'hamster' }, { name: 'cat', active: true }, { id: 3, name: 'dog' } ]

remove( array, key, value, from = 0 )

Removes the first occurrence where arr[n][key] == value and returns the index. If no match is found, -1 is returned.

findRemove( array, key, value, from = 0 )

Similar to remove(), but the actual item the is being removed is returned.

removeAll( array, key, value, from = 0 )

Removes the all occurrences where arr[n][key] == value and returns the index. An array of the removed items is returned.

diff( sArr, nArr )

Returns an object containing { added : [], removed : [], same : [] } where each of the added, removed and same array contains differences between sArr and nArr

← Object UtilString Util →
  • Install
  • index( array, key, value, from = 0 )
  • find( array, key, value, from = 0 )
  • replace( array, key, value, elem, from = 0 )
  • remove( array, key, value, from = 0 )
  • findRemove( array, key, value, from = 0 )
  • removeAll( array, key, value, from = 0 )
  • diff( sArr, nArr )
Leverege Platform
Docs
Overview
Connect
FacebookLinkedInTwitter
Facebook Open Source