1 | /** |
||
2 | * Highly efficient filter on a value in an object |
||
3 | * @param {Array} arr the array of objects |
||
4 | * @param {string} attr the attribute to filter on |
||
5 | * @param {mixed} value the value to compare |
||
6 | * @return {Array} the filtered array |
||
7 | */ |
||
8 | export function filter(arr, attr, value) { |
||
9 | const len = arr.length |
||
10 | let result = [] |
||
11 | let i = 0 |
||
12 | |||
13 | for (; i < len; i += 1) { |
||
14 | let elt = arr[i] |
||
15 | |||
16 | if (elt[attr] == value) { |
||
17 | result.push(elt) |
||
18 | } |
||
19 | } |
||
20 | return result |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * Highly efficient facet on a value in an object |
||
25 | * Works also for nested properties |
||
26 | * @param {Array} arr the array of objects to search |
||
27 | * @param {Array} attrs the attributes to facet on |
||
28 | * @param {mixed} value the value to compare |
||
29 | * @return {Array} the filtered array |
||
30 | */ |
||
31 | export function facet(arr, attrs, value) { |
||
32 | let searches = [] |
||
33 | Array.prototype.forEach.call(attrs, attr => { |
||
34 | let attrPath = attr.split('.') |
||
35 | searches.push(attrPath) |
||
36 | }) |
||
37 | |||
38 | const len = arr.length |
||
39 | let result = [] |
||
40 | let i = 0 |
||
41 | |||
42 | for (; i < len; i += 1) { |
||
43 | let elt = arr[i] |
||
44 | let found = false |
||
45 | Array.prototype.forEach.call(searches, search => { |
||
46 | let j = 0 |
||
47 | let a = arr[i] |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
48 | while (j < search.length) { |
||
49 | a = a[search[j]] |
||
50 | j++ |
||
51 | } |
||
52 | if (a.indexOf(value) !== -1) { |
||
53 | found = true |
||
54 | } |
||
55 | }) |
||
56 | |||
57 | if (found) { |
||
58 | result.push(elt) |
||
59 | } |
||
60 | } |
||
61 | |||
62 | return result |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Highly efficient find indexes on a value in a property of an object |
||
67 | * Works also for nested properties |
||
68 | * @param {Array} arr the array of objects |
||
69 | * @param {string} attr the attribute to filter on |
||
70 | * @param {mixed} value the value to compare |
||
71 | * @return {Array} the filtered array of indexes |
||
72 | */ |
||
73 | export function find(arr, attr, value) { |
||
74 | const len = arr.length |
||
75 | let result = [] |
||
76 | let i = 0 |
||
77 | |||
78 | for (; i < len; i += 1) { |
||
79 | let elt = arr[i] |
||
80 | |||
81 | if (elt[attr] == value) { |
||
82 | result.push(i) |
||
83 | } |
||
84 | } |
||
85 | return result |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Remove objects from an array given an attribute value |
||
90 | * @param {Array} arr the array of objects |
||
91 | * @param {string} attr the attribute to filter on |
||
92 | * @param {mixed} value the value to compare |
||
93 | * @return {Array} the array with corresponding objects removed |
||
94 | */ |
||
95 | export function removeByAttr(arr, attr, value) { |
||
96 | let i = arr.length |
||
97 | while (i--) { |
||
98 | if ( |
||
99 | arr[i] && |
||
100 | arr[i].hasOwnProperty(attr) && |
||
101 | (arguments.length > 2 && arr[i][attr] === value) |
||
102 | ) { |
||
103 | arr.splice(i, 1) |
||
104 | } |
||
105 | } |
||
106 | |||
107 | return arr |
||
108 | } |
||
109 | |||
110 | export function contains(arr, obj) { |
||
111 | var i = arr.length |
||
112 | while (i--) { |
||
113 | if (arr[i] === obj) { |
||
114 | return true |
||
115 | } |
||
116 | } |
||
117 | return false |
||
118 | } |
||
119 |