I am calling a function with two arguments,
Arg 1: Object {a:1, b:2, c:3, d:4}
Arg 2: Condition ((prop, key) => prop >= 3))
Here based on the condition we need to filter the object and provide the result as array of objects.
The code that I have tried,
JavaScript
x
6
1
const pickBy = (a, b) => {
2
const data = Object.values(a).filter(b);
3
console.log(data)
4
}
5
6
pickBy({a:1, b:2, c:3, d:4}, ((prop, key) => prop >= 3))
Current Result: [3,4]
Expected Result: [{c:3}, {d:4}]
Advertisement
Answer
You could get the entries, filter by handing over the right format for filtering function and build objects of filtered entries.
JavaScript
1
7
1
const
2
pickBy = (object, filterFn) => Object
3
.entries(object)
4
.filter(([k, v]) => filterFn(v, k))
5
.map(([k, v]) => ({ [k]: v }));
6
7
console.log(pickBy({ a: 1, b: 2, c: 3, d: 4 }, (prop, key) => prop >= 3));