4

I have two objects with unequal lengths, I want to merge based on 'mac_id'. I was working with lodash _.merge(), but it is not compatible with variable object lengths.

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 got the desired data but not in a structured way.

what methods do I need to follow to get the final result to be like this?

[ 
      { 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
  • Is the order of the items important? Commented Sep 28, 2017 at 13:22
  • you mean merging two arrays? Commented Sep 28, 2017 at 13:23
  • @floriangisse no the order of the arrangement is not important Commented Sep 28, 2017 at 13:37
  • Only the corresponding mac_ids from the obj4 and obj3 are to be grouped with either of key, value pairs of obj4 and obj3 Commented Sep 28, 2017 at 13:46

4 Answers 4

3

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);
Sign up to request clarification or add additional context in comments.

Comments

2

You can do it in the following way

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>

1 Comment

Thankyou @marvel308 Its working, can you please shed some light on .map(...)and ...result[e]
0

Here is a solution without using lodash. The library need not always be available, so here is a solution for completion.

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

Comments

0

Another way

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.