I have 2 arrays of data. I want to be able to "loop" through the first array, adding each item to a new "final array" in the same order, but for each item it checks, if it has the same ID as one of the items from a second array, then I want it to NOT add that item from the first array to the final array, but instead want it to add that matching ID item from the second array instead in its place.
If there are no items from the first array that match the item from the second array, that second array item will instead get added to the bottom of the final array.
Example:
// array 1
const mainArray = [
{name: 'example1', id: 1},
{name: 'example2', id: 2},
{name: 'example3', id: 3},
{name: 'example4', id: 4},
{name: 'example5', id: 5},
]
// array 2
const uploadArray = [
{name: 'floop', id: 2},
{name: 'example10', id: 10}
]
// adds all items from mainArray to finalArray, but since floop also had an id of 2,
//it got added in place of example2, since example10 didn't match any items in mainArray,
// it was added to finalArray after all of mainArray was added to finalArray
finalArray = [
{name: 'example1', id: 1},
{name: 'floop', id: 2},
{name: 'example3', id: 3},
{name: 'example4', id: 4},
{name: 'example5', id: 5},
{name: 'example10', id: 10}
]