ez-array-update
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

ez-array-update

Immutably update an element in an array

This library allows you to update an element in an array with immutability in mind

Install

$ npm install ez-array-update
$yarn add ez-array-update

Usage

You can update arbitrary data like numbers/strings

import updateArray from 'ez-array-update'

const sampleArray = [1, 2, 3, 4, 5]

// Change element `1` to 100
const filter = element => element === 1

const dataToUpdateTo = 100

const newNumberArray = updateArray(sampleArray, filter, dataToUpdateTo) // [100, 2, 3, 4, 5]

It also supports objects (Note: It will merge objects using spreads so it will only do a shallow merge)

import updateArray from 'ez-array-update'

const arrOfObjects = [
  { name: 'James', age: 21 },
  { name: 'Rob', age: 25 },
  { name: 'Tegan', age: 22 },
  { name: 'Alex', age: 21 },
  { name: 'Kacee', age: 25 }
]

const filter = element => element.name === 'James'

const data = { age: 240 } // You can pass in the keys you only wish to update

const newFriends = updateArray(arrOfObjects, filter, data)

Package Sidebar

Install

npm i ez-array-update

Weekly Downloads

4

Version

1.0.2

License

MIT

Unpacked Size

9.54 kB

Total Files

10

Last publish

Collaborators

  • stealtheritz