Skip to content

replace
161 bytes

Replace an item in an array

Usage

Given an array of items, replace the one that matches the given condition function. Only replaces the first match. Always returns a copy of the original array.

import * as _ from 'radashi'
const fish = [
{
name: 'Marlin',
weight: 105,
},
{
name: 'Bass',
weight: 8,
},
{
name: 'Trout',
weight: 13,
},
]
const salmon = {
name: 'Salmon',
weight: 22,
}
// read: replace fish with salmon where the name is Bass
const replaced = _.replace(fish, salmon, f => f.name === 'Bass') // => [marlin, salmon, trout]