Get the object array indexOf dublicate element in nodejs

511
January 16, 2017, at 7:39 PM

I have a big problem for me. I guess stackoverflow members can help me.. I have an excel file in my local. and than I want to read this excel and if excel rows have a duplicate data than I write to console. For example clearly;

It's my excel file: enter image description here

And than my reading excel row code:

workbook.xlsx.readFile('excelvalidation.xlsx').then(function(){
            var rowValues = {};
            var worksheet = workbook.getWorksheet(1);
            var columnCount = worksheet.columnCount;
                worksheet.eachRow(function(row, rowNumber){
                    if(rowNumber > 2){
                        var dizi = [];
                        row.eachCell(function(cell, colNumber){
                            dizi.push(cell.value);
                        });
                        rowValues[rowNumber-3] = dizi;
                    }

                });
                for(var i = 0 ; i < Object.keys(rowValues).length ; i++){
                        console.log(" " + rowValues[i]);
                }

        });

rowValues is object what have a all row. it means 1. row is rowValues first element and 2. row is rowValues second element vs...

So in excel file row 5. and 11. duplicate and I want to write console for message "5 and 11 row is duplicate" what I want. How can I do it? Thanks for helps..

Answer 1

You can calculate an md5 hash for each item in rowValues, by concatenating the values in the array into a single string, then using md5 function. This hash can then be used as the index to the rows which had these values (the values which resulted in that md5 hash).

Here's a sample implementation that can detect multiple occurrences (more than 2 duplicate rows):

var rowValues = [
  ['E', '10-15', 'ankara'],
  ['E', '10-15', 'antalya'],
  ['E', '20-25', 'ankara'],
  ['E', '20-25', 'antalya'],
  ['K', '10-15', 'ankara'],
  ['K', '10-15', 'antalya'],
  ['K', '20-25', 'ankara'],
  ['K', '20-25', 'antalya'],
  ['E', '20-25', 'ankara'],
  ['K', '10-15', 'antalya'],
  ['E', '20-25', 'ankara'],
];
var hashes = {};
rowValues.forEach(function(row, idx){
  var hash = md5(row.join('~~~'));
  if (hash in hashes) {
    hashes[hash].push(idx);
  } else {
    hashes[hash] = [idx];
  }
})
Object.keys(hashes).forEach(function(key, idx) {
  var msg = '';
  if (hashes[key].length > 1) {
    msg = 'Rows ' + hashes[key].join(' and ') + ' are duplicate\n';
    console.log(msg);
  }
});

Demo here: https://jsfiddle.net/b5nqbavq/2/

READ ALSO
Learnyounode juggling async - solution returns undefined

Learnyounode juggling async - solution returns undefined

I am new to nodejs and have been trying my hands on "learnyounode" workshop from nodeschoolio

439
How to mock require in electronJS

How to mock require in electronJS

I'm trying to write unit test for my electron js application and I'm stuck at one place

479
mongoose, node, cannot use the method defined in user.model.js

mongoose, node, cannot use the method defined in user.model.js

I am using node and mongoose to build an appAnd when testing, there exists a strange problem

318
DynamoDB equivelent to Find({}).toArray

DynamoDB equivelent to Find({}).toArray

I'm looking to export an entire table in DynamoDB as an array of objectsI'm recently converting from MongoDB, and in that I'd use

461