Codepen:http://codepen.io/giorgiomartini/pen/jEvaxZ
我有这个Api:
http://private-5d90c-kevinhiller.apiary-mock.com/angular_challenge/horror_movies
这样的结构如下:
正如您所看到的那样,有offers
数组,其中有一个provider_id,我只想获取提供者2中的电影。
{
"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"
},
...
我如何只获得具有provider_id 2的电影?用lodash?
感谢。
答案 0 :(得分:1)
试试这个
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();
答案 1 :(得分:1)
var all = yourArrayOfObjects;
var onlyWant = _.filter(yourArrayOfObjects, function(item) {
return _.any(item.offers, function(offer) {
return offer.provider_id === 2;
});
}