1

I want to merge two arrays from the db into one without overwriting the data, I have tried different options with lodash merge, concatenate and other but without luck. Can you please help!

My objects:

places: [
    { placeId: 2, place: 'a' },
    { placeId: 3, place: 'b' },
    { placeId: 4, place: 'c' },
    { placeId: 5, place: 'd' },
    { placeId: 6, place: 'e' }
  ]

places: [
    { placeId: 12, place: 'f' },
    { placeId: 13, place: 'g' },
    { placeId: 14, place: 'h' },
    { placeId: 21, place: 'i' },
    { placeId: 22, place: 'j' },
    { placeId: 29, place: 'k' },
    { placeId: 30, place: 'l' }
  ]



i want to get this

places:[
    { placeId: 2, place: 'a' },
    { placeId: 3, place: 'b' },
    { placeId: 4, place: 'c' },
    { placeId: 5, place: 'd' },
    { placeId: 6, place: 'e' },
    { placeId: 12, place: 'f' },
    { placeId: 13, place: 'g' },
    { placeId: 14, place: 'h' },
    { placeId: 21, place: 'i' },
    { placeId: 22, place: 'j' },
    { placeId: 29, place: 'k' },
    { placeId: 30, place: 'l' }


]

Josip Marić
  • 227
  • 4
  • 21

1 Answers1

2

you can do like this:

const allPlaces = places1.concat(places2)

or

const allPlaces = [...places1,...places2]

IsKat
  • 64
  • 3