-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathfilter_unique.js
49 lines (42 loc) · 914 Bytes
/
filter_unique.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* Return news array containing only the unique items
* found in input array.
*
* IMPORTANT: Note that items are considered unique
* if `String({})` is unique. For example;
*
* Lib.filterUnique([ { a: 1 }, { b: 2 } ])
*
* returns [{ a: 1 }]
*
* and
*
* Lib.filterUnique([ '1', 1 ])
*
* returns ['1']
*
*
* @param {array} array base array
* @return {array} new filtered array
*/
module.exports = function filterUnique(array) {
var seen = {},
out = [],
j = 0;
for(var i = 0; i < array.length; i++) {
var item = array[i];
if(seen[item] !== 1) {
seen[item] = 1;
out[j++] = item;
}
}
return out;
};