I am working with GPS related program so I am actively receiving many location objects. This is the array form I would want to achieve. Initially it should be an empty array and then start with only one child, all incoming location objects would be stored in that one child until some other command is issued. When next event starts, all incoming objects would be stored in second child array, hope you get the gists.
rootArray = [
[
{lat: '', long: ''},
{lat: '', long: ''},
{lat: '', long: ''},
],
[
{lat: '', long: ''},
{lat: '', long: ''},
{lat: '', long: ''},
]
]
This is what I have tried but did not succeed.
// I am working with redux here actually
// rootArray: []
case NEW_LOCATION_OBJECT_COMING:
return {
...state,
rootArray: state.rootArray[state.rootArray.length].concat(action.payload.locationObject),
}
concat
topush
?state.rootArray[state.rootArray.length].push(action.payload.locationObject)
push
that will mutate the array,concat
on the other hand is a good method that returns a new array.