/
findKey.js
40 lines (35 loc) · 921 Bytes
/
findKey.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
// findKey({
// "Blue Hill": { stars: 1 },
// "Akaleri": { stars: 3 },
// "noma": { stars: 2 },
// "elBulli": { stars: 3 },
// "Ora": { stars: 2 },
// "Akelarre": { stars: 3 }
// }, x => x.stars === 2) // => "noma"
// const findKey = function(obj, fun) {
// for(let key in obj) {
// if(fun(obj[key])) {
// return key;
// }
// }
// }
const findKey = function(obj, fun) {
const multKeys = [];
for (let key in obj) {
if (fun(obj[key])) {
multKeys.push(key);
return multKeys;
//console.log('OBJ & KEY: ' + JSON.stringify(obj[key]))
}
}
//return multKeys;
};
module.exports = findKey;
// console.log(findKey({
// "Blue Hill": { stars: 1 },
// "Akaleri": { stars: 3 },
// "noma": { stars: 2 },
// "elBulli": { stars: 3 },
// "Ora": { stars: 2 },
// "Akelarre": { stars: 3 }
// }, x => x.stars === 2)); // => "noma"