0

I've read some posts where duplicates are removed from an array. For example, an array with "one", "one", "two", "three" will return "one", "two", "three" using the code I've found in some posts. I need to remove both values so "one", "one", "two", "three", will return "two", "three". I'm parsing a file and searching for items based on a regular expression. Here's what I have for code. There are times though where there is duplicate data in the file I'm parsing. I need to remove that data.

function parseText(text)
    {
        var records = text.match(fileRegex);
        var fileArray = [];


        if (records != null) {
            records.forEach(function (element, index, array) {
                var matches = elementRegex.exec(element);

                if (matches != null) {
                    var year = matches[parseSettings.ParseYearPosition];
                    if (year == 2)
                    {
                        year = "20" + year;
                    }
                    var journalDate = new Date(year,
                        matches[parseSettings.ParseMonthPosition] - 1,
                        matches[parseSettings.ParseDayPosition],
                        matches[parseSettings.ParseHourPosition],
                        matches[parseSettings.ParseSecondPosition], 0, 0);

                    var file = {
                        Id: null,
                        ImportDate: moment(importDate).format(),
                        JournalDate: moment(journalDate).format(),
                        TraceNumber: parseInt(matches[parseSettings.ParseTraceNumberPosition]),
                        Amount: parseFloat(matches[parseSettings.ParseAmountPosition]),
                        Deleted: false,
                        Dirty: true
                    };
                    fileArray.push(file);

                }
            });

           deferred.resolve(fileArray);
        }
rsford31
  • 145
  • 1
  • 1
  • 9
  • possible duplicate of [Remove duplicates from an array of objects in javascript](http://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) – TaoPR Jun 10 '15 at 15:25

1 Answers1

0

You can use a simple for-loop to check whether fileArray[i] == fileArray[i+1] and if so splice them from the array.

update

So the for loop before you resolve fileArray would look like:

for(var i = 1; i <= fileArray.length;) {
    if(fileArray[i] == fileArray[i-1]) {
        fileArray.splice(i-1, 2);
        i+=2;
    }
    else
        i++;
}
Guinn
  • 1,355
  • 13
  • 29
  • I'm not sure that will work. It's an array of objects. And splice will remove one item where I need both. So fileArray[0] will be '{Id: null, ImportDate: ImportDate = "2015-06-10T11:32:24-05:00", TraceNumber: 1111, Amount: 1.11, Deleted:false, Dirty: true}' and fileArray[1] may also be '{Id: null, ImportDate: ImportDate = "2015-06-10T11:32:24-05:00", TraceNumber: 1111, Amount: 1.11, Deleted:false, Dirty: true}' so I need to punt both fileArray[0] and fileArray[1] – rsford31 Jun 10 '15 at 16:37
  • You can tell splice how many items you want to remove starting from index i, so if fileArray[i] == fileArray[i+1] you can do fileArray.splice(i, 2) – Guinn Jun 11 '15 at 07:04
  • @rsford31 check my updated answer, it should do what you want! – Guinn Jun 11 '15 at 07:12