Answers for "merge array lodash by id"

0

how to merge 2 object array by the same key with lodash

Lodash
var a = [
  { userId:"p1", item:1},
  { userId:"p2", item:2},
  { userId:"p3", item:4}
];

var b = [
  { userId:"p1", profile:1},
  { userId:"p2", profile:2},
  { userId:"p4", profile:4}
];
var merged = _.merge(_.keyBy(a, 'userId'), _.keyBy(b, 'userId'));
var values = _.values(merged);
console.log(values);
<script src="https://cdnjs.cloudflare.com/ajax/libs/
Posted by: Guest on June-26-2020
-1

merge two arrays of objects lodash

const array1 = [{id:1, name:'doc1'}, {id:2, name:'doc2', {id: 3: "test"}}];
const array2 = [{id:1, name:'doc1'}, {id:3, name:'doc3'}, {id:4, name:'doc4'}];

const result = _.unionBy(array1, array2, 'id');
console.log(result);
Posted by: Guest on July-22-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language