Combining two objects of different lengths, keys and values

I have two objects with unequal length, I want to merge based on "mac_id". I worked with lodash _.merge(), but it is incompatible with the lengths of variable objects.

obj4=[ 
  { mac_id: '00-17-0d-00-00-30-47-fa',
    sent_mail_time: '2017-09-28 11:07:0' },
  { mac_id: '00-17-0d-00-00-30-48-18',
    sent_mail_time: '2017-09-28 11:07' }
    ];

obj3=[ 
  { mac_id: '00-17-0d-00-00-30-4d-94',
    notif_time: '2017-09-28 10:16:28' },
  { mac_id: '00-17-0d-00-00-30-47-fa',
    notif_time: '2017-09-28 10:14:28' },
  { mac_id: '00-17-0d-00-00-30-49-58',
    notif_time: '2017-09-28 10:26:28' } ]

Using _.groupBy('mac_id'), I received the necessary data, but not structured.

What methods should be used to get the final result?

[ 
      { mac_id: '00-17-0d-00-00-30-4d-94',
        notif_time: '2017-09-28 10:16:28' },
      { mac_id: '00-17-0d-00-00-30-47-fa',
        sent_mail_time: '2017-09-28 11:07:0',
        notif_time: '2017-09-28 10:14:28' },
      { mac_id: '00-17-0d-00-00-30-49-58',
        notif_time: '2017-09-28 10:26:28' },
        { mac_id: '00-17-0d-00-00-30-48-18',
        sent_mail_time: '2017-09-28 11:07' } ]
+4
source share
4 answers

You can use Object.assign ():

var _ = require('lodash');

let obj4=[
    { mac_id: '00-17-0d-00-00-30-47-fa',
        sent_mail_time: '2017-09-28 11:07:0' },
    { mac_id: '00-17-0d-00-00-30-48-18',
        sent_mail_time: '2017-09-28 11:07' }
];

let obj3=[
    { mac_id: '00-17-0d-00-00-30-4d-94',
        notif_time: '2017-09-28 10:16:28' },
    { mac_id: '00-17-0d-00-00-30-47-fa',
        notif_time: '2017-09-28 10:14:28' },
    { mac_id: '00-17-0d-00-00-30-49-58',
        notif_time: '2017-09-28 10:26:28' } ];


obj3.forEach(obj3Child => {
    let objtToMerge;
    if (!(objtToMerge = _.find(obj4,function (obj4Child) { return obj4Child.mac_id === obj3Child.mac_id; }))) {
        obj4.push(obj3Child);
    } else {
        Object.assign(objtToMerge, obj3Child);
    }
});

console.log(obj4);
+3
source

You can do it as follows

obj4=[ 
      { mac_id: '00-17-0d-00-00-30-47-fa',
        sent_mail_time: '2017-09-28 11:07:0' },
      { mac_id: '00-17-0d-00-00-30-48-18',
        sent_mail_time: '2017-09-28 11:07' }
     ];

obj3=[ 
      { mac_id: '00-17-0d-00-00-30-4d-94',
         notif_time: '2017-09-28 10:16:28' },
      { mac_id: '00-17-0d-00-00-30-47-fa',
         notif_time: '2017-09-28 10:14:28' },
      { mac_id: '00-17-0d-00-00-30-49-58',
         notif_time: '2017-09-28 10:26:28' }
   ];
   
let result= _u.groupBy(obj4.concat(obj3), 'mac_id');

result = Object.keys(result).map(e => Object.assign({}, ...result[e]));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script>
_u = _.noConflict(); // lets call ourselves _u
</script>
Run codeHide result
+2
source

lodash. , .

var obj4=[ 
	{ mac_id: '00-17-0d-00-00-30-47-fa',
		sent_mail_time: '2017-09-28 11:07:0' },
	{ mac_id: '00-17-0d-00-00-30-48-18',
		sent_mail_time: '2017-09-28 11:07' }
];

var obj3=[ 
	{ mac_id: '00-17-0d-00-00-30-4d-94',
		notif_time: '2017-09-28 10:16:28' },
	{ mac_id: '00-17-0d-00-00-30-47-fa',
		notif_time: '2017-09-28 10:14:28' },
	{ mac_id: '00-17-0d-00-00-30-49-58',
		notif_time: '2017-09-28 10:26:28' } ]

var reducer = function(obj1, obj2) {
	return obj1.reduce( (i, j) => {
		var temp = obj2.filter( o => o.mac_id == j.mac_id);
		i.push(temp == []? j: Object.assign({}, j, temp[0]) );
		return i;
	}, []);
};

function removeDuplicates(originalArray, prop) {
	// https://stackoverflow.com/a/38595329/4110233
	var newArray = [];
	var lookupObject  = {};

	for(var i in originalArray) {
		lookupObject[originalArray[i][prop]] = originalArray[i];
	}

	for(i in lookupObject) {
		newArray.push(lookupObject[i]);
	}
	return newArray;
}

var v1 = reducer(obj3, obj4);
var v2 = reducer(obj4, obj3);

console.log(removeDuplicates([...v1, ...v2], "mac_id"));
Hide result
0

// merge objs
merged = obj3.concat(obj4).map( (item,index,arr) => {
    let duplicates = arr.filter( v => v.mac_id == item.mac_id  )
    return Object.assign(...duplicates)
})

// remove duplicates
merged =  [...new Set(merged)] 
console.log(merged) 
0

Source: https://habr.com/ru/post/1686542/


All Articles