I have an array of object like this:
channels=[
{name: mega, status: true},
{name: ant, status: false},
{name: apl, status: true}
]
and I have a single object with this format
obj = {0: false, 1: true}
The keys in the plain object represent indexes of the channels array. The status properties must be updated.
For the above example data channels should be updated to:
channels=[
{name: mega, status: false},
{name: ant, status: true},
{name: apl, status: true}
]
How can I implement this efficiently?
You can iterate through the
objwith any method you like (here I usedObject.keysto get an array of keys fromobjobject andforEachto iterate through them) and update the field. It can be achieved in one line of code:In the provided case, the original array is mutated, and if it's not what you need, I recommend to take a look at
mapmethod, it doesn't mutate the original array, it creates a new one instead.