0
var records = [ 
   { 
      "defaultContact":true,
      "contactName":"testContactName",
      "mobileNumber":"900000000000",
      "mobileDialCode":"+91 IN",
      "faxNumber":"123",
      "faxDialCode":"+91 IN",
      "emailId":"test@gmail.com"
   },
   { 
      "defaultContact":false,
      "contactName":"xyz",
      "mobileNumber":"900000001000",
      "mobileDialCode":"+91 IN",
      "faxNumber":"123",
      "faxDialCode":"+91 IN",
      "emailId":"xyz@gmail.coma"
   },
   { 
      "defaultContact":false,
      "contactName":"asdasd",
      "mobileNumber":"123",
      "mobileDialCode":"+91 IN",
      "faxNumber":"",
      "faxDialCode":"",
      "emailId":""
   },
   { 
      "contactName":"asdasd",
      "defaultContact":false,
      "emailId":"",
      "faxDialCode":"",
      "faxNumber":"",
      "mobileDialCode":"+91 IN",
      "mobileNumber":"123"
   }
];

The above is an array of object i have done this using two for loops but this doesn't look good, can anyone suggest how to do it with ES6 Higher order functions.

here duplicate means when each & every property matched exactly same.

below is how i did it:

let duplicateRecords = [];
    for (let i = 0; i < records.length; i++) {
      for (let j = i + 1; j < records.length; j++) {
        if (
          records[i].contactName === records[j].contactName &&
          records[i].emailId === records[j].emailId &&
          records[i].faxDialCode === records[j].faxDialCode &&
          records[i].faxNumber === records[j].faxNumber &&
          records[i].mobileDialCode === records[j].mobileDialCode &&
          records[i].mobileNumber === records[j].mobileNumber
        ) {
          duplicateRecords = [records[j]];
        }
      }
    }

Any help would be appreciated.

Nakul Nagariya
  • 97
  • 1
  • 1
  • 8
  • Thanks @Prabhjot Singh Kainth No, i want the duplicate object if there is any. – Nakul Nagariya Jan 02 '20 at 11:07
  • Change `duplicateRecords = [records[j]];` to `duplicateRecords.push(records[j]);`. – Felix Kling Jan 02 '20 at 11:16
  • Thanks @FelixKling for the suggestion but i want this to be converted and done by using ES6 functions, anyway the above was working but if you can suggest something could be achieved by using Array.every() or something like that. – Nakul Nagariya Jan 02 '20 at 11:20
  • FWIW, `Array.every` is already part of ES5. `every` wouldn't make sense in your case since you want to get the duplicate records back. `every` returns a boolean. – Felix Kling Jan 02 '20 at 11:32

1 Answers1

1

In your case using loop for is more appropriate than high-order function. But you can use Object.keys and Array.filter to make your code more universal and short.

for (let i = 0; i < records.length; i++) {
   const keys = Object.keys(records[i]);
     for (let j = i + 1; j < records.length; j++) {
         const isDublicated = !Boolean(keys.filter(key => records[i][key] !== records[j][key]).length);
         isDublicated ? dublicatedRecords.push(records[j]) : null;
     }
}

Or using high-order functions:

const dublicatedRecords = records.reduce((acc, record, index) => {
  const keys = Object.keys(record);
  records.forEach((otherRecord, otherIndex) => {
      const isDublicated = !Boolean(keys.filter(key => record[key] !== otherRecord[key]).length);
      isDublicated && index !== otherIndex ? acc.push(otherRecord) : null;
  })
  return acc;
}, []);