Lodash filter and functional composition

I have an array of objects with a key mediaand a value, which may or may not be empty. I have a function to select this key value and a function that checks that the value is empty, so that mediaEmptyComparer({media: ''})returns true and mediaEmptyComparer({media: 'somevalue'})false.

    var array = _.times(100, function () { return {media: Math.random() < 0.5 ? '' : 'value'} });

    var mediaPicker        = _.partialRight(_.get, 'media');
    var mediaEmptyComparer = _.flow(mediaPicker, _.isEmpty);

    var splittedArray = _.filter(array, mediaEmptyComparer);
    console.log(splittedArray);

When I try to use a function like the predicate for _.filter, it always fails (as in the example), but if I write:

    var splittedArray = _.filter(array, function (x) { return mediaEmptyComparer(x);});

it works. This is similar to the fact that the value for each iteration is not passed as an argument to the function. Any help? Thanks

+4
source share
1 answer

_.filter() : value, index | key collection.
_.ary(), , .
https://lodash.com/docs#ary

var splittedArray = _.filter(array, _.ary(mediaEmptyComparer, 1));

: https://github.com/lodash/lodash/issues/844

!

+5

Source: https://habr.com/ru/post/1618415/


All Articles