Completed
Push — master ( 3ff50b...307302 )
by greg
03:33
created

array.js ➔ find   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
dl 0
loc 14
rs 9.4285
nop 3
1
/**
2
 * Highly efficient filter on a value in an object
3
 * @param  {Array} arr   the array of objects
4
 * @param  {string} key   the key to filter on
5
 * @param  {mixed} value the value to compare
6
 * @return {Array}       the filtered array
7
 */
8
export function filter(arr, key, value) {
9
  var result = [];
10
  var i = 0;
11
  var len = arr.length;
12
13
  for (; i < len; i += 1) {
14
    var elt = arr[i];
15
16
    if (elt[key] == value) {
17
      result.push(element);
0 ignored issues
show
Bug introduced by
The variable element seems to be never declared. If this is a global, consider adding a /** global: element */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
18
    }
19
  }
20
  return result;
21
}
22
23
/**
24
 * Highly efficient find indexes on a value in an property of an object
25
 * @param  {Array} arr   the array of objects
26
 * @param  {string} key   the key to filter on
27
 * @param  {mixed} value the value to compare
28
 * @return {Array}       the filtered array of indexes
29
 */
30
export function find(arr, key, value) {
31
  var result = [];
32
  var i = 0;
33
  var len = arr.length;
34
35
  for (; i < len; i += 1) {
36
    var elt = arr[i];
37
38
    if (elt[key] == value) {
39
      result.push(i);
40
    }
41
  }
42
  return result;
43
}
44