Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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/
Comment

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);
Comment

lodash merge array of objects without duplicates

var c = _.map(a, function(obj) {
    return _.assign(obj, _.find(b, {parentId: obj.aId}));
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: findbyid mongoose 
Javascript :: get bottom position of element javascript 
Javascript :: laravel link custom javascript file 
Javascript :: spawn template playcanvas 
Javascript :: how to add field to object in js 
Javascript :: open sans font react js 
Javascript :: get data attribute javascript 
Javascript :: js array backwards 
Javascript :: Javascript removing duplicates in array 
Javascript :: query string from object js 
Javascript :: react select npm 
Javascript :: Add an element to an array at a specific index with JavaScript 
Javascript :: continuous scrolling js 
Javascript :: axios post form data and json 
Javascript :: export all javascript 
Javascript :: vue mounted 
Javascript :: short ajax get method jquery 
Javascript :: vuejs check object key exist 
Javascript :: toastr alert js 
Javascript :: create an object array in js 
Javascript :: string uppercase 
Javascript :: How To Open Phone Dialer and Make Call From React Native App 
Javascript :: make copy of date javascript 
Javascript :: how to install vue 
Javascript :: react slick 
Javascript :: nodejs delete s3 folder 
Javascript :: check upload img extension jquery 
Javascript :: span change jquery 
Javascript :: javascript generator function 
Javascript :: javascript element onblur 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =