-3

For example consider the following array with objects.

var collection = [{name:'user1',phone:'203'},
                  {name:'user2',phone:'5050'},
                  {name:'user1',phone:'203'}]

Now I want to compare the each object with each other and give the result as.

var newCollection =   {name:'user1',phone:'203', rowMatch:'true'},
                      {name:'user2',phone:'5050', rowMatch:'false'},
                      {name:'user1',phone:'203',rowMatch:'true'}

So, I want the new collecition like this where it compares and updates with new property when object properties match like the first and third object.

Ajay
  • 25
  • 1
  • 5
  • you mean if 2 or more rows are same, you need to set the `rowMatch` to true ? – Dane Nov 07 '17 at 17:22
  • Even if one row matches with another you have to update both the object with rowMatch : true. – Ajay Nov 07 '17 at 19:11

4 Answers4

1
var newCollection = collection.map(item => {
   item.rowMatch = !!collection.find(i => i !== item && i.phone === item.phone && i.name === item.name);
   return item;
});

Here you're just using map to iterate through and check if each item has a duplicate that isn't the original.

bryan60
  • 28,215
  • 4
  • 48
  • 65
0

Create a hash function for your object type.

A simple concatenation of the fields seems to work in this case. Iterate through your collection and for each element, hash it, and put it in a table.

Keep track of the counts. When the count is greater than 1 you know you have a duplicate (row match)

JavaScript Hashmap Equivalent

WillW
  • 43
  • 6
0

use Array.prototype.find

var newCollection = collection.map((row, index) => {
       if(row["rowMatch"])
          return row; 

      rowMatch = !!collection.find((r,i) => i !== index && r.name === row.name && r.phone === row.phone); 
      row["rowMatch"] = rowMatch;

      return row; 

})
DesTroy
  • 390
  • 4
  • 17
  • this will always find the item you're currently on, need to exclude it. and your find usage is off, find doesn't return -1 if not found, it returns undefined. – bryan60 Nov 07 '17 at 17:32
  • @bryan60, yup saw that now, returns `undefined`, using double not now. – DesTroy Nov 07 '17 at 17:36
0

You can use newCollection or manipulate in the collection like this

collection.forEach((item)=>{
item.rowMatch = (collection.filter((e)=>{return (e.name==item.name&&e.phone==item.phone)}).length>1)?'true':'false';
})
console.log(collection)

Simple is that.Here is working JSFiddle for it https://jsfiddle.net/touqeer/pgdsw9Le/1/ .

Touqeer
  • 150
  • 1
  • 10
  • @Rick Jolly No this will not.You can see working fiddle in edited answer now.Thanks – Touqeer Nov 07 '17 at 18:24
  • Find will not let you know the length.The basic logic is to find more iteration of an object. – Touqeer Nov 07 '17 at 18:25
  • My bad, didn't notice the `length > 1`. Deleted my comments to avoid confusion. – Rick Jolly Nov 07 '17 at 18:29
  • Thanks man.If you have tried this then upvote.Thanks in advance. – Touqeer Nov 07 '17 at 18:31
  • Yeah it works but would rather see `item.rowMatch = (condition)` or `item.rowMatch = (condition) ? 'true' : 'false'` if strings are necessary. I also don't prefer the filter inefficiency and direct mutation though. – Rick Jolly Nov 07 '17 at 18:37
  • this is less performant for an already poor performance operation, filter demands that you continue checking even after a match is found rather than stopping when 2 are found. Find should be used instead. – bryan60 Nov 07 '17 at 18:41
  • Answer updated as you want but i think filter have to be there as we would have to iterate on all items for taking length of matched items – Touqeer Nov 07 '17 at 18:41
  • this method works, but it is worse performance than using find. The big O with this method is always n^2, with a find operation instead, the big O is significantly lower. – bryan60 Nov 07 '17 at 18:43
  • Find gets the value of the first element in the array that has a matched value. – Touqeer Nov 07 '17 at 18:45
  • Thanks @Ajay you are awesome.If this was helpful than please accept the answer and give it an upvote.Thanks again man. – Touqeer Nov 07 '17 at 18:47
  • yes, which is why you just need to add a simple check to make sure you're not looking at the same object. Then it stops iterating after it finds a match that isn't the current object. Much better performance. Your method does needless iteration. – bryan60 Nov 07 '17 at 19:09