javascript - Remove duplicates in an array of objects -
i've read posts duplicates removed array. example, array "one", "one", "two", "three" return "one", "two", "three" using code i've found in posts. need remove both values "one", "one", "two", "three", return "two", "three". i'm parsing file , searching items based on regular expression. here's have code. there times though there duplicate data in file i'm parsing. need remove 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); }
you can use simple for-loop check whether filearray[i] == filearray[i+1] , if splice them array.
update
so loop before resolve filearray like:
for(var = 1; <= filearray.length;) { if(filearray[i] == filearray[i-1]) { filearray.splice(i-1, 2); i+=2; } else i++; }
Comments
Post a Comment