This update method don't mutate your array, but will create a new array as a result, for update the array, we replace the object inside that array with new one and then the method will create and return to a new array, so you can assign its value to your state or data.
Copy $.array.update(fromArray, updateObject, byPropOrIndex)
// Example:
const fromArray = [
{id: 1, name: 'Zulkifli Usman', age: 63},
{id: 2, name: 'Razali Usman', age: 28},
{id: 3, name: 'Bukhari Usman', age: 25},
{id: 4, name: 'Boyhaki Usman', age: 21}
]
$.array.update(fromArray, {name: 'Tedir Ghazali'}, {id: 2})
//=> [..., {id: 2, name: 'Tedir Ghazali', age: 28}, ...]
//or by using its index
$.array.update(fromArray, {name: 'Tedir Ghazali'}, 1)
//=> [..., {id: 2, name: 'Tedir Ghazali', age: 28}, ...]
Or you may want to update array of objects by multiple properties or indexes, Alga.js have a helper for that kind of problem, you can use updateBy
helper, this helper will apply its updated object to some properties or indexes.
Copy $.array.updateBy(fromArray, putObject, ...allPropOrIndex*)
//Example:
const fromArray = [
{ id: '1', name: 'Hanbal Tedir' },
{ id: '2', name: 'Dawoud Tedir' },
{ id: '3', name: 'Harist Tedir' },
{ id: '4', name: 'Tedir Ghazali' },
{ id: '5', name: 'Hanbal Usman' },
{ id: '6', name: 'Dawoud Usman' },
{ id: '7', name: 'Harist Usman' },
{ id: '8', name: 'Ghazali Usman' },
{ id: '9', name: 'Bukhari Usman' },
{ id: '10', name: 'Boyhaki Usman' },
{ id: '11', name: 'Zulkifli Usman' },
{ id: '12', name: 'Teuku Usman' }
]
$.array.updateBy(fromArray, {deleted: true}, [{id: '1'}, {id: '3'}, {id: '7'}, {id: '10'}, {id: '12'}])
//=> [
// { id: '1', name: 'Hanbal Tedir', deleted: true },
// { id: '2', name: 'Dawoud Tedir' },
// { id: '3', name: 'Harist Tedir', deleted: true },
// { id: '4', name: 'Tedir Ghazali' },
// { id: '5', name: 'Hanbal Usman' },
// { id: '6', name: 'Dawoud Usman' },
// { id: '7', name: 'Harist Usman', deleted: true },
// { id: '8', name: 'Ghazali Usman' },
// { id: '9', name: 'Bukhari Usman' },
// { id: '10', name: 'Boyhaki Usman'deleted: true },
// { id: '11', name: 'Zulkifli Usman' },
// { id: '12', name: 'Teuku Usman', deleted: true }
//]
//or by some indexes
$.array.updateBy(fromArray, {updated: false}, [1, 3, 4, 5, 7, 9])
//=> [
// { id: '1', name: 'Hanbal Tedir' },
// { id: '2', name: 'Dawoud Tedir', updated: false },
// { id: '3', name: 'Harist Tedir' },
// { id: '4', name: 'Tedir Ghazali', updated: false },
// { id: '5', name: 'Hanbal Usman', updated: false },
// { id: '6', name: 'Dawoud Usman', updated: false },
// { id: '7', name: 'Harist Usman' },
// { id: '8', name: 'Ghazali Usman', updated: false },
// { id: '9', name: 'Bukhari Usman' },
// { id: '10', name: 'Boyhaki Usman', updated: false },
// { id: '11', name: 'Zulkifli Usman' },
// { id: '12', name: 'Teuku Usman' }
//]