1.0.2 • Published 5 years ago

ez-array-update v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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)
1.0.2

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago