-3

This function returns diff between two objects , i need to modify it to return common objects. Any help is appreciated.

Array sample:

var array1 = [{
  "Name": "Single",
  "URL": "xxx",
  "ID": 123
}, {
  "Name": "Double",
  "URL": "yyy",
  "ID": 888
}, {
  "Name": "Triple",
  "URL": "zzz",
  "ID": 567
}];

var arrar2 = [{
  "Name": "Single",
  "URL": "xxx",
  "ID": 123
}, {
  "Name": "Double",
  "URL": "yyy",
  "ID": 888
}, {
  "Name": "index",
  "URL": "zzz",
  "ID": 567
}];

// expected result
var resultArray = [{
  "Name": "Single",
  "URL": "xxx",
  "ID": 123
}, {
  "Name": "Double",
  "URL": "yyy",
  "ID": 888
},
}];

Current code:

function objDiff(array1, array2) {
  var resultArray = []

  array2.forEach(function(destObj) {
    var check = array1.some(function(origObj) {
      if (origObj.name == destObj.name) return true
    })
    if (!check) {
      destObj.desc = 'missing in source'
      resultArray.push(destObj)
    }
  })

  array1.forEach(function(origObj) {
    var check = array2.some(function(destObj) {
      if (origObj.name == destObj.name) return true
    })
    if (!check) {
      origObj.desc = 'missing in destination'
      resultArray.push(origObj)
    }
  })

  return resultArray
}
YakovL
  • 7,557
  • 12
  • 62
  • 102
Gagan Deep
  • 89
  • 1
  • 10
  • How about also supplying some sample objects to test it with. – Scott Marcus Jul 27 '17 at 19:53
  • 1
    You need to give some indication on what your current thought process is. We need to know that you are thinking critically about your problem. In its current state you will lose all of your rep because this sounds like "I have to modify this code, but I have no clue how, please do my work for me". Rather than, "I'm curious about x, y, z and I've tried a, b, and c". – marcusshep Jul 27 '17 at 19:56
  • I have added sample objects – Gagan Deep Jul 27 '17 at 20:06

3 Answers3

0

That's not your code, but the following function will return all matches by exploring both arrays with two forEach() loops. Algorithm complexity is given by array1.length * array2.length. Don't use for large arrays! But it's the easiest way to think of it. Indeed the first think that comes to my mind is checking every element of array2 for every element of array1 and compare them.

var array1 = ['DETE', 'Ivany', 'James', 'Don', 'Crakcer']
var array2 = ['Jamies', 'Ivanyy', 'DETE', 'Don']

function objMatch(array1,array2) {
  
   var matches = [];
  
   array1.forEach(function(element1) {
   
      array2.forEach(function(element2) {
      
         if(element1 == element2) {
            
            matches.push(element1);
            
         }
      
      });
   
   });

  return matches;

}

console.log(objMatch(array1, array2));
// will return ['DETE', 'Don'] 

Another way to do with only one loop is to use sort(), credit to jeremy

var array1 = ["cat", "sum","fun", "run", "gut"];
var array2 = ["bat", "cat","dog","sun", "hut", "gut"];

var arrayMatch = function(array1, array2) {

  var matches = [];
  
  array1.sort();
  array2.sort();
  
  for (var i = 0; i < array1.length; i += 1) {
  
    if (array2.indexOf(array1[i]) > -1) {
      matches.push(array1[i]);
    }
  }
  
  return matches;

}
console.log(arrayMatch(array1,array2))

And yet another way to do it is by using Array.prototype.filter, credit to Paul S.

var array1 = ['DETE', 'Ivany', 'James', 'Don', 'Crakcer']
var array2 = ['Jamies', 'Ivanyy', 'DETE', 'Don']

function arrayMatch(array1, array2) {
    var t;
    if (array1.length > array2.length) t = array2, array2 = array1, array1 = t;
    
    return array1.filter(function (e) {
        return array2.indexOf(e) > -1;
    });
}

console.log(arrayMatch(array1, array2));
Ivan
  • 34,531
  • 8
  • 55
  • 100
0

If all you want is to look for things that are the same in both arrays, you only need to loop over one of them. Something along these lines should work:

function objSame(array1, array2) {
    var resultArray = []

    array2.forEach(function(destObj) {
        var check = array1.some(function(origObj) {
            if(origObj.name == destObj.name) return true
        })
        if(check) {
            destObj.desc = 'Same in both'
            resultArray.push(destObj)
        }
    })
    return resultArray
}
Daniel
  • 3,312
  • 1
  • 14
  • 31
0

To find array elements that have a common Name property value, you could use a Map to avoid O(n²) time complexity. That map would have the objects from the first array keyed by their name. Pass it as the this object to a filter on the second array:

function objCommon(array1, array2) {
    return array2.filter(function (obj) {
        return this.has(obj.Name);
    }, new Map(array1.map(obj => [obj.Name, obj])));
}

var array1= [
  { "Name": "Single", "URL": "xxx", "ID": 123 }, 
  { "Name": "Double", "URL": "yyy", "ID": 888}, 
  { "Name": "Triple", "URL": "zzz", "ID": 567 }];

var array2= [
  { "Name": "Single", "URL": "xxx", "ID": 123 }, 
  { "Name": "Double", "URL": "yyy", "ID": 888 }, 
  { "Name": "index", "URL": "zzz", "ID": 567 }];

var result = objCommon(array1, array2);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
trincot
  • 317,000
  • 35
  • 244
  • 286