Javascript对象-通过对象和过滤器值进行映射

时间:2020-03-16 06:55:21

标签: javascript arrays node.js object mapping

当前,我能够检索Netflix提供的节目列表。

但是我的分数过滤器没有返回我想要的。 我想返回tmdb:score为8或更大的列表。 由于数据结构(存储在searchResult变量中)正在拆分provider_type和value,因此我现在得到的得分都在8分以上。

有没有办法做到这一点? 如果可能的话,将其与我的Netflix提供程序功能结合以返回tmdb:score为8或更高的Netflix节目?

谢谢。

Node JS文件

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){
         movie.offers.forEach(offer => {
             if(offer.provider_id === netflixId){
                 print_result("search", searchResult)
             }
         })
     })


    searchResult.items.map(function(movie){
        movie.scoring.filter(score =>{
            if(score.provider_type ==="tmdb:score" && score.value > 8) {
                print_result("search", searchResult)
            }
        })
    })

}) ();



数据结构(存储在searchResult变量中,该对象包含一个计数total_results和一个对象数组items,每个对象代表一部电影)

{
    "total_results": 27476,
    "items": [
        {
            "jw_entity_id": "ts80908",
            "id": 80908,
            "title": "A Very English Scandal",
            "full_path": "/us/tv-show/a-very-english-scandal",
            "full_paths": {
                "SHOW_DETAIL_OVERVIEW": "/us/tv-show/a-very-english-scandal"
            },
            "poster": "/poster/60368458/{profile}",
            "original_release_year": 2018,
            "tmdb_popularity": 3.422,
            "object_type": "show",
            "offers": [
                {
                    "type": "aggregated",
                    "monetization_type": "flatrate",
                    "provider_id": 9,
                    "currency": "USD",
                    "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",
                    "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"
                }
            ],
            "scoring": [
                {
                    "provider_type": "imdb:score",
                    "value": 7.8
                },
                {
                    "provider_type": "tmdb:popularity",
                    "value": 3.422
                },
                {
                    "provider_type": "tmdb:score",
                    "value": 8.1
                }
            ]
        },
        {
            "jw_entity_id": "tm205151",
            "id": 205151,
            "title": "Alpha",
            "full_path": "/us/movie/alpha-2015",
            "full_paths": {
                "MOVIE_DETAIL_OVERVIEW": "/us/movie/alpha-2015"
            },
....

3 个答案:

答案 0 :(得分:0)

据我了解,您想过滤tmdb:score为8或更高的Netflix节目吗?那么我们可以只使用filter来过滤项目吗?

searchResult.items.filter(function(movie){
    var scorings = movie.scoring;
    var filterScores = 
        scorings.filter(score => score.provider_type ==="tmdb:score" && score.value > 8);
    return filterScores.length > 0;
})

答案 1 :(得分:0)

如果您只想过滤一个tmdb:score> = 8.0的电影对象数组,请使用以下代码段进行演示(请注意,我使用items数组来模拟与您相同格式的对象只是两个几乎完全相同的对象,一个用于演示目的,其得分> 8.0,另一个与得分<8.0,得分:

const searchResult = {
    "total_results": 27476,
    "items": [
        {
            "jw_entity_id": "ts80908",
            "id": 80908,
            "title": "A Very English Scandal",
            "full_path": "/us/tv-show/a-very-english-scandal",
            "full_paths": {
                "SHOW_DETAIL_OVERVIEW": "/us/tv-show/a-very-english-scandal"
            },
            "poster": "/poster/60368458/{profile}",
            "original_release_year": 2018,
            "tmdb_popularity": 3.422,
            "object_type": "show",
            "offers": [
                {
                    "type": "aggregated",
                    "monetization_type": "flatrate",
                    "provider_id": 9,
                    "currency": "USD",
                    "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",
                    "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"
                }
            ],
            "scoring": [
                {
                    "provider_type": "imdb:score",
                    "value": 7.8
                },
                {
                    "provider_type": "tmdb:popularity",
                    "value": 3.422
                },
                {
                    "provider_type": "tmdb:score",
                    "value": 8.1
                }
            ]
        },{
            "jw_entity_id": "ts80909",
            "id": 80908,
            "title": "Some Other Title",
            "full_path": "/us/tv-show/a-very-english-scandal",
            "full_paths": {
                "SHOW_DETAIL_OVERVIEW": "/us/tv-show/a-very-english-scandal"
            },
            "poster": "/poster/60368458/{profile}",
            "original_release_year": 2018,
            "tmdb_popularity": 3.422,
            "object_type": "show",
            "offers": [
                {
                    "type": "aggregated",
                    "monetization_type": "flatrate",
                    "provider_id": 9,
                    "currency": "USD",
                    "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",
                    "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"
                }
            ],
            "scoring": [
                {
                    "provider_type": "imdb:score",
                    "value": 7.8
                },
                {
                    "provider_type": "tmdb:popularity",
                    "value": 3.422
                },
                {
                    "provider_type": "tmdb:score",
                    "value": 7.9
                }
            ]
        }]
 }

 const desiredMovies = searchResult.items.filter(movie => {
 	let tmdbScoring = movie.scoring.find(scoringObj => scoringObj.provider_type==="tmdb:score");
 	return tmdbScoring && tmdbScoring.value>=8.0;
 })

 console.log(desiredMovies);

此外,我注意到您添加的代码中似乎存在问题,这可能解释了为什么要获得所有电影。

在下面的代码中,您为发现的每个成功结果打印一次searchResult,这是一个包含计数和整个数组items的对象,因此将显示您可以看到数据结构中的所有电影。

//searchResult is the object that contains the entire array "items"
searchResult.items.map(function(movie){
    movie.scoring.filter(score =>{
        if(score.provider_type ==="tmdb:score" && score.value > 8) {
            //the variable searchResult has scope here so it will contain the entire list
            print_result("search", searchResult)
        }
    })
})

免责声明:以下内容仅是建设性的批评。

此外,我可以肯定您对.map.filter的使用是不正确的。提供给这些函数的回调需要返回一个布尔值,以便.map.filter函数本身可以返回一个新数组(要注意,.map.filter不能影响原始数组)。提供的回调不仅不会返回布尔值,而且映射或过滤器本身的结果也不会存储在变量中。因此,我建议您看一下这些高阶函数的文档,以更好地了解它们的工作原理:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

因此,我建议您创建一个函数,该函数从对象的items列表中返回已过滤的电影列表,并一起打印。

答案 2 :(得分:0)

            "SELECT
                   p.*,

                    pr.unlimited as 'unlimited'
                FROM
                   `broadcast_portal` as p INNER JOIN broadcast_portal_registration as pr on p.id=pr.portal_id
                WHERE
                    pr.group_id = 49
                HAVING (unlimited = true or (select MAX(cpl._limit) FROM commercial_portal_limit as cpl INNER JOIN user on cpl.commercial_id=user.id INNER JOIN user_user_group uug ON user.id = uug.user_id WHERE uug.group_id=49 AND cpl.portal_id=p.id) > 0)
                ORDER BY p.name ASC "
        );

        $qb->execute();
        return $qb->fetchAll();

const netflixId = 8

const searchResult = {
    "items": [
        {
            "offers": [
                {
                    "type": "aggregated",
                    "monetization_type": "flatrate",
                    "provider_id": 8,
                    "currency": "USD",
                    "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",
                    "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"
                }
            ],
            "scoring": [
                {
                    "provider_type": "imdb:score",
                    "value": 7.8
                },
                {
                    "provider_type": "tmdb:popularity",
                    "value": 3.422
                },
                {
                    "provider_type": "tmdb:score",
                    "value": 8.1
                }
            ]
        },
        {
            "offers": [
                {
                    "type": "aggregated",
                    "monetization_type": "flatrate",
                    "provider_id": 8,
                    "currency": "USD",
                    "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",
                    "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"
                }
            ],
            "scoring": [
                {
                    "provider_type": "imdb:score",
                    "value": 7.8
                },
                {
                    "provider_type": "tmdb:popularity",
                    "value": 3.422
                },
                {
                    "provider_type": "tmdb:score",
                    "value": 7.9
                }
            ]
        }
]}
        
        
const movies = searchResult.items.filter(item => {
  return item.offers.some(offer => offer.provider_id === netflixId) && item.scoring.some(score => score.provider_type === 'tmdb:score'&&score.value>=8)
})

console.log(movies)

此过滤器功能可根据2个条件过滤出项目。

  1. 在商品报价中,报价之一必须来自netflix
  2. 在项目评分中,得分之一必须为tmdb:score,且得分必须大于或等于8。

希望有帮助。