I want to write my own iteratee for lodash differenceBy that give me different array values greater than 5.
According documentation. The iteratee is used for "generate the criterion by which they're pared."
Example according documentation:
_.differenceBy([1, 2, 3, 5, 6], [1, 2, 3, 8, 10], Math.floor); // [5, 6]
This will be similar using Math.floor()
let iter_floor = (value) => {
return Math.floor(value);
};
let differenceBy = _.differenceBy([1, 2, 3, 5, 6], [1, 2, 3, 8, 10], iter_floor);
console.log(differenceBy); // [5, 6]
But when I try this
let iter_greater = (value) => {
return value > 5;
};
let differenceBy = _.differenceBy([1, 2, 3, 5, 6], [1, 2, 3, 8, 10], iter_greater);
console.log(differenceBy); // []
I get an empty array. I would expect to obtain different array values greater than 5
The source code of lodash differenceBy is here: .17.5/lodash.js#L6971
Could you give me an example how to write a iteratee for this case.
Thanks.
I want to write my own iteratee for lodash differenceBy that give me different array values greater than 5.
According documentation. The iteratee is used for "generate the criterion by which they're pared."
Example according documentation:
_.differenceBy([1, 2, 3, 5, 6], [1, 2, 3, 8, 10], Math.floor); // [5, 6]
This will be similar using Math.floor()
let iter_floor = (value) => {
return Math.floor(value);
};
let differenceBy = _.differenceBy([1, 2, 3, 5, 6], [1, 2, 3, 8, 10], iter_floor);
console.log(differenceBy); // [5, 6]
But when I try this
let iter_greater = (value) => {
return value > 5;
};
let differenceBy = _.differenceBy([1, 2, 3, 5, 6], [1, 2, 3, 8, 10], iter_greater);
console.log(differenceBy); // []
I get an empty array. I would expect to obtain different array values greater than 5
The source code of lodash differenceBy is here: https://github./lodash/lodash/blob/4.17.5/lodash.js#L6971
Could you give me an example how to write a iteratee for this case.
Thanks.
Share Improve this question asked Apr 24, 2018 at 16:35 Luis ReinosoLuis Reinoso 7584 silver badges13 bronze badges 2-
I'm still slightly unclear on what your desired result is. Can you list it explicitly? I'm guessing you wanted just
[6]
? – CRice Commented Apr 24, 2018 at 16:48 - Yes, In the example the result should be [6] – Luis Reinoso Commented Apr 24, 2018 at 18:34
3 Answers
Reset to default 4By using a function with a parison, you create two arrays with boolean values.
This
_.differenceBy([1, 2, 3, 5, 6], [1, 2, 3, 8, 10], iter_greater);
works in two steps:
Map all values and take the iteratee function
iter_greater
[1, 2, 3, 5, 6] -> [false, false, false, false, true] [1, 2, 3, 8, 10] -> [false, false, false, true, true]
Filter the mapped first array by checking if the value exists in the second array.
[false, false, false, false, true] first array [false, false, false, true, true] second array false exists in 2nd array false exists in 2nd array false exists in 2nd array false exists in 2nd array true exists in 2nd array [ ] // no values, 2nd array contains true and false
differenceBy
will run each value through that function, and then if the return value is unique, will keep it. Your iter_greater
returns only true or false, neither of which occur only once, so it (as it should) returns an empty array.
You could do it like this, which will work in most instances:
let iter_greater = (value) => {
return value > 5 ? value : undefined;
};
let differenceBy = _.differenceBy([1, 2, 3, 5, 6], [1, 2, 3, 8, 10], iter_greater);
console.log(differenceBy); // []
<script src="https://cdn.jsdelivr/npm/[email protected]/lodash.min.js"></script>
But you'll have issues with:
let iter_greater = (value) => {
return value > 5 ? value : undefined;
};
let differenceBy = _.differenceBy([1, 2, 3, 5, 6], [], iter_greater);
console.log(differenceBy); // []
<script src="https://cdn.jsdelivr/npm/[email protected]/lodash.min.js"></script>
What you probably want to do, is just chain together _.difference
and .filter
:
let differenceBy = _.difference([1, 2, 3, 5, 6], [1, 2, 3, 8, 10]).filter(x => x > 5);
console.log(differenceBy); // []
<script src="https://cdn.jsdelivr/npm/[email protected]/lodash.min.js"></script>
Use this
let iter_floor = (value) => {
return value > 5 && Math.floor(value);
};
let differenceBy = _.differenceBy([1, 2, 3, 5, 6,7,8,9], [1, 2, 3, 8, 10], iter_floor);
console.log(differenceBy); // [6, 7, 9]