【问题标题】:Javascript Map through Array of Objects通过对象数组的 Javascript 映射
【发布时间】:2023-03-18 23:33:01
【问题描述】:

我能够打印出 tmdb_popularity,但无法使用 provider(netflix) 进行映射/打印。 我相信我通过 {} 映射不正确。 movie.offers[0].provider_id 也失败了。

另外,有没有办法仅通过 'provider_type:tmdb:score' > 8 来绘制评分? 由于 tmdb:score 是 Object.value 而不是 key 不可行吗?

谢谢。

const JustWatch = require ("justwatch-api")

const netflixId = 8;

function print_result (name, result) {
    console.log(name+":");
    console.log(JSON.stringify(result, null, 4));
    console.log("\n\n\n\n");
}

(async function(){
    var justwatch = new JustWatch();

    var searchResult = await justwatch.search('a');

    searchResult.items.map(function(movie){
        if(movie.offers.provider_id == netflixId){
            print_result("search", searchResult)
        }
    })

     searchResult.items.map(function(movie){
         if(movie.tmdb_popularity > 20){
             print_result("search",searchResult)
         }
    })

}) ();

来自 (JustWatch API) 的 JSON 数据是什么样的

{
    "total_results": 27468,
    "items": [
        {
            "jw_entity_id": "ts80908",
            "id": 80908,
            "title": "A Very English Scandal",
            "tmdb_popularity": 3.422,
            "object_type": "show",
            "offers": [
                {
                    "type": "aggregated",
                    "monetization_type": "flatrate",
                    "provider_id": 9,
                    "currency": "USD",
                    "urls": {
                        "standard_web": "https://www.amazon.com/gp/product/B07DX9DKP5?camp=1789&creativeASIN=B07DX9DKP5&ie=UTF8&linkCode=xm2&tag=justwatch09-20",
                        "deeplink_android_tv": "intent://watch.amazon.com/watch?asin=B07DX9DKP5&time=0&territory=US#Intent;package=com.amazon.amazonvideo.livingroom;scheme=https;end"
                    },
                    "subtitle_languages": [
                        "en"
                    ],
                    "presentation_type": "sd",
                    "element_count": 1,
                    "new_element_count": 1,
                    "date_provider_id": "2019-09-14_9",
                    "date_created": "2019-09-14"
                },
                {
                    "type": "aggregated",
                    "monetization_type": "flatrate",
                    "provider_id": 9,
                    "currency": "USD",
                    "urls": {
                        "standard_web": "https://www.amazon.com/gp/product/B07DX9DKP5?camp=1789&creativeASIN=B07DX9DKP5&ie=UTF8&linkCode=xm2&tag=justwatch09-20",
                        "deeplink_android_tv": "intent://watch.amazon.com/watch?asin=B07DX9DKP5&time=0&territory=US#Intent;package=com.amazon.amazonvideo.livingroom;scheme=https;end"
                    },
                    "subtitle_languages": [
                        "en"
                    ],
                    "presentation_type": "hd",
                    "element_count": 1,
                    "new_element_count": 1,
                    "date_provider_id": "2019-09-14_9",
                    "date_created": "2019-09-14"
                },
                {
                    "type": "aggregated",
                    "monetization_type": "flatrate",
                    "provider_id": 9,
                    "currency": "USD",
                    "urls": {
                        "standard_web": "https://www.amazon.com/gp/product/B07DX8CSSQ?camp=1789&creativeASIN=B07DX8CSSQ&ie=UTF8&linkCode=xm2&tag=justwatch09-20",
                        "deeplink_android_tv": "intent://watch.amazon.com/watch?asin=B07DX8CSSQ&time=0&territory=US#Intent;package=com.amazon.amazonvideo.livingroom;scheme=https;end",
                        "deeplink_fire_tv": "intent://com.amazon.tv.launcher/detail?provider=aiv&providerId=B07DX8CSSQ#Intent;package=com.amazon.tv.launcher;scheme=amzn;end",
                        "deeplink_tvos": "aiv://aiv/watch?asin=B07DX8CSSQ&territory=US&time=auto"
                    },
                    "subtitle_languages": [
                        "en"
                    ],
                    "presentation_type": "4k",
                    "element_count": 1,
                    "new_element_count": 1,
                    "date_provider_id": "2019-09-06_9",
                    "date_created": "2019-09-06"
                }
            ],
            "scoring": [
                {
                    "provider_type": "tmdb:score",
                    "value": 8.1
                },
                {
                    "provider_type": "imdb:score",
                    "value": 7.8
                },
                {
                    "provider_type": "tmdb:popularity",
                    "value": 3.422
                }
            ]
        },
        {
            "jw_entity_id": "tm205151",
            "id": 205151,
            "title": "Alpha",
            "full_path": "/us/movie/alpha-2015",
            "full_paths": {
                "MOVIE_DETAIL_OVERVIEW": "/us/movie/alpha-2015"
            },
            "poster": "/poster/139203407/{profile}",
            "original_release_year": 2015,
            "tmdb_popularity": 0.878,
            "object_type": "movie",
            "offers": [
                {
                    "monetization_type": "flatrate",
                    "provider_id": 25,
                    "urls": {
                        "standard_web": "https://www.fandor.com/films/alpha"
                    },
                    "presentation_type": "hd",
                    "date_provider_id": "2016-06-13_25",
                    "date_created": "2016-06-13"
                },
                {
                    "monetization_type": "flatrate",
                    "provider_id": 25,
                    "urls": {
                        "standard_web": "https://www.fandor.com/films/alpha"
                    },
                    "presentation_type": "sd",
                    "date_provider_id": "2016-06-13_25",
                    "date_created": "2016-06-13"
                },
                {
                    "monetization_type": "flatrate",
                    "provider_id": 199,
                    "currency": "USD",
                    "urls": {
                        "standard_web": "https://www.amazon.com/gp/product/B01M7O3UQU?camp=1789&creativeASIN=B01M7O3UQU&ie=UTF8&linkCode=xm2&tag=justwatch09-20",
                        "deeplink_android_tv": "intent://watch.amazon.com/watch?asin=B01M7O3UQU&time=0&territory=US#Intent;package=com.amazon.amazonvideo.livingroom;scheme=https;end",
                        "deeplink_fire_tv": "intent://com.amazon.tv.launcher/detail?provider=aiv&providerId=B01M7O3UQU#Intent;package=com.amazon.tv.launcher;scheme=amzn;end",
                        "deeplink_tvos": "aiv://aiv/watch?asin=B01M7O3UQU&territory=US&time=auto"
                    },
                    "presentation_type": "sd",
                    "date_provider_id": "2019-06-19_199",
                    "date_created": "2019-06-19"
                }
            ],
            "scoring": [
                {
                    "provider_type": "tmdb:popularity",
                    "value": 0.878
                },
                {
                    "provider_type": "tmdb:id",
                    "value": 333465
                },
                {
                    "provider_type": "tmdb:score",
                    "value": 7
                },
                {
                    "provider_type": "imdb:score",
                    "value": 7.7
                }
            ]
        },
        ....

【问题讨论】:

  • movie.offers 是一个数组,它需要另一个循环,例如用于searchResult.items
  • 另外.map() 用于返回示例代码中未使用的新数组,您可以改用.forEach()
  • 我同意@Matt。更清楚地定义您要对 JSON 数据执行的操作可能会更好。关于你的第二个问题,我不太确定你在问什么,但遍历评分数组以找到与 provider_type 对应的分数:“tmbd_score”应该很安静。
  • @marksy_91 我该怎么做?我是否使用对象键/值功能?

标签: javascript arrays node.js object mapping


【解决方案1】:

这里试试这个

searchResult.items.map(function(movie){
        movie.offers.forEach(offer => {
          if(offer.provider_id == netflixId){
             print_result("search", searchResult)
          }
        })
    })

【讨论】:

    【解决方案2】:

    forEach 在这里比map 更好,因为您没有在 map 函数中返回。使用查找功能搜索报价。

    searchResult.items.forEach(function(movie) {
        const offer = movie.offers.find(offer => offer.provider_id == netflixId);
        if(offer){
            print_result("search", movie)
            print_result("offer", offer)
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-24
      • 2022-01-25
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多