通过对象数组的Javascript映射

时间:2020-03-16 05:21:18

标签: javascript arrays node.js object mapping

我能够打印出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
                }
            ]
        },
        ....

2 个答案:

答案 0 :(得分:0)

在这里尝试

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

答案 1 :(得分:0)

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)
    }
})