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' } ]
SamForiYana
  • 53
  • 1
  • 2
  • 11

4 Answers4

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);
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>
marvel308
  • 10,288
  • 1
  • 21
  • 32
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"));
TheChetan
  • 4,440
  • 3
  • 32
  • 41
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) 
Brendan Doherty
  • 148
  • 2
  • 6