0
var records = [ 
   { 
      "defaultContact":true,
      "contactName":"testContactName",
      "mobileNumber":"900000000000",
      "mobileDialCode":"+91 IN",
      "faxNumber":"123",
      "faxDialCode":"+91 IN",
      "emailId":"[email protected]"
   },
   { 
      "defaultContact":false,
      "contactName":"xyz",
      "mobileNumber":"900000001000",
      "mobileDialCode":"+91 IN",
      "faxNumber":"123",
      "faxDialCode":"+91 IN",
      "emailId":"[email protected]"
   },
   { 
      "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.

4
  • Thanks @Prabhjot Singh Kainth No, i want the duplicate object if there is any. Commented Jan 2, 2020 at 11:07
  • Change duplicateRecords = [records[j]]; to duplicateRecords.push(records[j]);. Commented Jan 2, 2020 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. Commented Jan 2, 2020 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. Commented Jan 2, 2020 at 11:32

1 Answer 1

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

2 Comments

Pretty much same but thanks for adding some ES6 flavor to it, that's what i wanted. do you know anything like can we done in lodash, just this looping doesn't look good.
updated answer with solution without for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.