-
Notifications
You must be signed in to change notification settings - Fork 0
/
HOFrefactoring.js
76 lines (59 loc) · 2.22 KB
/
HOFrefactoring.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Get all elements but the Nth
function getAllElementsButNth(array, n) {
return array.filter(function(value, i) {
return i !== n;
});
}
// Write a function called "getElementsThatEqual10AtProperty".
//
// Given an object and a key, "getElementsThatEqual10AtProperty" returns an array containing all the elements of the array located at the given key that are equal to ten.
function getElementsThatEqual10AtProperty(obj, key) {
if (obj.hasOwnProperty(key) && Array.isArray(obj[key])) {
return obj[key].filter(function(value) {
return value === 10;
});
}
return [];
}
// Write a function called "select".
//
// Given an array and an object, "select" returns a new object whose properties are those in the given object AND whose keys are present in the given array.
function select(arr, obj) {
var newObj = {};
arr.forEach(function(value, i) {
if (obj.hasOwnProperty(arr[i])) {
newObj[arr[i]] = obj[arr[i]];
}
});
return newObj;
}
// Write a function called "getElementsLessThan100AtProperty".
//
// Given an object and a key, "getElementsLessThan100AtProperty" returns an array containing all the elements of the array located at the given key that are less than 100.
function getElementsLessThan100AtProperty(obj, key) {
if(obj.hasOwnProperty(key) && Array.isArray(obj[key])) {
return obj[key].filter(function(value, i) {
return value < 100;
});
}
return [];
}
// Write a function called "getElementsGreaterThan10AtProperty".
//
// Given an object and a key, "getElementsGreaterThan10AtProperty" returns an array containing the elements within the array, located at the given key, that are greater than 10.
function getElementsGreaterThan10AtProperty(obj, key) {
if(obj.hasOwnProperty(key) && Array.isArray(obj[key])) {
return obj[key].filter(function(value, i) {
return value > 10;
});
}
return [];
}
// Write a function called "removeElement".
//
// Given an array of elements, and a "discarder" parameter, "removeElement" returns an array containing the items in the given array that do not match the "discarder" parameter.
function removeElement(array, discarder) {
return array.filter(function(value, i) {
return value !== discarder;
});
}