gpt4 book ai didi

javascript - 如何使用lodash仅获取Json文件中数组内具有特定属性的对象?

转载 作者:行者123 更新时间:2023-12-03 10:37:37 25 4
gpt4 key购买 nike

代码笔: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的电影?与洛达什?

谢谢。

最佳答案

var all = yourArrayOfObjects;

var onlyWant = _.filter(yourArrayOfObjects, function(item) {
return _.any(item.offers, function(offer) {
return offer.provider_id === 2;
});
}

关于javascript - 如何使用lodash仅获取Json文件中数组内具有特定属性的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28928210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com