Array
Replace an item in an array
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 { replace } from 'radash'
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
replace(fish, salmon, f => f.name === 'Bass') // => [marlin, salmon, trout]