Switch to array uniquing function that keeps the first instance

The previous version would keep only the last instance.

This version requires the array to contain only primitives of a single
data type, but I think that's OK for all of our uses. (This version
should also be faster.)
This commit is contained in:
Dan Stillman 2014-05-07 03:10:39 -04:00
parent 42c02526ef
commit d65ee27592

View File

@ -604,23 +604,19 @@ Zotero.Utilities = {
/** /**
* Return new array with duplicate values removed * Return new array with duplicate values removed
* *
* From the JSLab Standard Library (JSL) * From http://stackoverflow.com/a/1961068
* Copyright 2007 - 2009 Tavs Dokkedahl
* Contact: http://www.jslab.dk/contact.php
* *
* @param {Array} array * @param {Array} array
* @return {Array} * @return {Array}
*/ */
"arrayUnique":function(arr) { "arrayUnique":function(arr) {
var a = []; var u = {}, a = [];
var l = arr.length; for (var i=0, l=arr.length; i<l; ++i){
for(var i=0; i<l; i++) { if (u.hasOwnProperty(arr[i])) {
for(var j=i+1; j<l; j++) { continue;
// If this[i] is found later in the array
if (arr[i] === arr[j])
j = ++i;
} }
a.push(arr[i]); a.push(arr[i]);
u[arr[i]] = 1;
} }
return a; return a;
}, },