So whenever presented with a set of conditions, say 1, 2, 3, never just go if (1) else if (2) else if (3) because that often results in unnecessary code and/or unnecessary computing.
Think about this condition:
If value is an empty string, delete the given prop property from the album.
It makes a lot of sense to test this first doesnât it? none of the other conditions want an empty string, and by testing this first, you eliminate additional checks later.
Now on to this:
If prop
isnât tracks
and value
isnât an empty string, update or set that albumâs prop
to value
.
so say you test this with if (props != 'tracks')
then you do not need to test if prop is âtracksâ later right? The only alternative to this if statement is if props is equal to âtracksâ
So now we got 3 checks, first we see if value is an empty string, if not we see if props is not âtracksâ, then else.
in the else you can put an if/else checking to see if the object[id]
already has the track
property.
Up until now itâs only been optimization, but here, with how you assign/change value to the track
property, is where you went wrong.
track
is an array, meaning if the property does not exist, you should initialize it as an array, then push value into it, instead of changing it to a string (value)
if the property does exist, you should push value into it, instead of changing it to a string (valueďźas well.