0

Codepen: http://codepen.io/giorgiomartini/pen/jEvaxZ

I have this Api:

http://private-5d90c-kevinhiller.apiary-mock.com/angular_challenge/horror_movies

which as this structure like this:

As you can see there is an offers array, which has a provider_id, I just want to get the movies that are in provider 2 for example.

  {
    "id": 140524,
    "title": "Dracula Untold",
    "poster": "https://images.justwatch.com/poster/298962/s332",
    "full_path": "https://www.justwatch.com/us/movie/dracula-year-zero",
    "object_type": "movie",
    "original_release_year": 2014,
    "offers": [
    {
    "monetization_type": "buy",
    "provider_id": 2,
    "retail_price": 14.99,
    "currency": "USD",
    "urls": {
    "standard_web": "https://itunes.apple.com/us/movie/dracula-untold/id921386678?uo=4"
    },
    "presentation_type": "hd"
    },
    {
    "monetization_type": "buy",
    "provider_id": 2,
    "retail_price": 14.99,
    "currency": "USD",
    "urls": {
    "standard_web": "https://itunes.apple.com/us/movie/dracula-untold/id921386678?uo=4"
    },
    "presentation_type": "sd"
    },
...

How can i get only the movies that have the provider_id 2 for example ? with lodash ?

Thanks.

0

2 Answers 2

1
var all = yourArrayOfObjects;

var onlyWant = _.filter(yourArrayOfObjects, function(item) {
    return _.any(item.offers, function(offer) {
        return offer.provider_id === 2;
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

var data = _.chain(data)
    .map(function(el) {
        el.offers = _.chain(el.offers)
            .uniq('provider_id')
            .filter(function(offer) {
                return offer.provider_id === 2;
            })
            .value();

        return el;
    })
    .filter(function(el) {
        return el.offers && el.offers.length;
    })
    .value();

Example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.