commit d65ee2759282349d14f5b1591254a3da5d874a59 parent 42c02526efb22fa52401ddb7808278d757ff6d29 Author: Dan Stillman <dstillman@zotero.org> Date: Wed, 7 May 2014 03:10:39 -0400 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.) Diffstat:
| M | chrome/content/zotero/xpcom/utilities.js | | | 16 | ++++++---------- |
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js @@ -604,23 +604,19 @@ Zotero.Utilities = { /** * Return new array with duplicate values removed * - * From the JSLab Standard Library (JSL) - * Copyright 2007 - 2009 Tavs Dokkedahl - * Contact: http://www.jslab.dk/contact.php + * From http://stackoverflow.com/a/1961068 * * @param {Array} array * @return {Array} */ "arrayUnique":function(arr) { - var a = []; - var l = arr.length; - for(var i=0; i<l; i++) { - for(var j=i+1; j<l; j++) { - // If this[i] is found later in the array - if (arr[i] === arr[j]) - j = ++i; + var u = {}, a = []; + for (var i=0, l=arr.length; i<l; ++i){ + if (u.hasOwnProperty(arr[i])) { + continue; } a.push(arr[i]); + u[arr[i]] = 1; } return a; },