Created
June 6, 2017 12:56
-
-
Save guilhermepontes/f52bf1e432d575c781b551431a644fb1 to your computer and use it in GitHub Desktop.
Merge 2 array of objects with Lodash Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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