Skip to content

Instantly share code, notes, and snippets.

@guilhermepontes
Created June 6, 2017 12:56
Show Gist options
  • Save guilhermepontes/f52bf1e432d575c781b551431a644fb1 to your computer and use it in GitHub Desktop.
Save guilhermepontes/f52bf1e432d575c781b551431a644fb1 to your computer and use it in GitHub Desktop.
Merge 2 array of objects with Lodash Raw
import { map, assign, mapKeys } from 'lodash'
const state = [
{ id: 1, coords: [4, 5, 6] },
{ id: 2, coords: [4, 5, 6] },
{ id: 6, coords: [4, 5, 6] },
{ id: 7, coords: [4, 5, 6] },
{ id: 8, coords: [4, 5, 6] }
]
const server = [
{ id: 1, coords: [1, 2, 3] },
{ id: 2, coords: [1, 2, 3] },
{ id: 3, coords: [1, 2, 3] },
{ id: 4, coords: [1, 2, 3] },
{ id: 5, coords: [1, 2, 3] },
]
const data = map(assign(
mapKeys(state, k => k.id),
mapKeys(server, k => k.id)
))
console.log(data)
/*
[[object Object] {
coords: [1, 2, 3],
id: 1
}, [object Object] {
coords: [1, 2, 3],
id: 2
}, [object Object] {
coords: [1, 2, 3],
id: 3
}, [object Object] {
coords: [1, 2, 3],
id: 4
}, [object Object] {
coords: [1, 2, 3],
id: 5
}, [object Object] {
coords: [4, 5, 6],
id: 6
}, [object Object] {
coords: [4, 5, 6],
id: 7
}, [object Object] {
coords: [4, 5, 6],
id: 8
}]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment