Replace an item in an array by a match function condition. If no items match the function condition, appends the new item to the end of the list.
const fish = [ { name: 'Marlin', weight: 105 }, { name: 'Salmon', weight: 19 }, { name: 'Trout', weight: 13 }]const salmon = { name: 'Salmon', weight: 22}const sockeye = { name: 'Sockeye', weight: 8}replaceOrAppend(fish, salmon, f => f.name === 'Salmon') // => [marlin, salmon (weight:22), trout]replaceOrAppend(fish, sockeye, f => f.name === 'Sockeye') // => [marlin, salmon, trout, sockeye] Copy
const fish = [ { name: 'Marlin', weight: 105 }, { name: 'Salmon', weight: 19 }, { name: 'Trout', weight: 13 }]const salmon = { name: 'Salmon', weight: 22}const sockeye = { name: 'Sockeye', weight: 8}replaceOrAppend(fish, salmon, f => f.name === 'Salmon') // => [marlin, salmon (weight:22), trout]replaceOrAppend(fish, sockeye, f => f.name === 'Sockeye') // => [marlin, salmon, trout, sockeye]
Description
Replace an item in an array by a match function condition. If no items match the function condition, appends the new item to the end of the list.
Example