【问题标题】:Query category via JSON with Javascript使用 Javascript 通过 JSON 查询类别
【发布时间】:2021-01-26 23:37:03
【问题描述】:

我有这个 JSON 文件。我只想查询赛后类别,但我需要最后两个。

    {
    "videos" : [
        {
            "url" : "https://www.youtube.com/watch?v=DSorwgunfqo",
            "category" : "Pre-Game",
            "team" : ["Rodholfo Silva"],
            "date" : "January 20th 2021"
        },
        {
            "url" : "https://www.youtube.com/watch?v=qFzxV0Dc2rw",
            "category" : "Post-Game",
            "team" : ["Rod Patrón"],
            "date" : "January 21st 2021"
        },
        {
            "url" : "https://www.youtube.com/watch?v=nq0358cJb-M",
            "category" : "Post-Game",
            "team" : ["Rod Patrón"],
            "date" : "January 17th 2021"
        },
        {
            "url" : "https://www.youtube.com/watch?v=iALl-ce5dzY",
            "category" : "Pre-Game",
            "team" : ["Rodholfo Silva"],
            "date" : "January 16th 2021"
        },
        {
            "url" : "https://www.youtube.com/watch?v=UY4WvV3dHNY",
            "category" : "Enterviews",
            "team" : ["Mathew Francis, Rodholfo Silva, Rod Patrón"],
            "date" : "July 16th 2020"
        },
        {
            "url" : "https://www.youtube.com/watch?v=7E2hMsmdbd4",
            "category" : "Post-Game",
            "team" : ["Rod Patrón"],
            "date" : "January 24th 2021"
        }
    ]
}

我正在使用 Javascript,通过 for 循环和 slice(1).slice(-2),但没有成功。

【问题讨论】:

    标签: javascript json for-loop


    【解决方案1】:

    const data = [
            {
                "url" : "https://www.youtube.com/watch?v=DSorwgunfqo",
                "category" : "Pre-Game",
                "team" : ["Rodholfo Silva"],
                "date" : "January 20th 2021"
            },
            {
                "url" : "https://www.youtube.com/watch?v=qFzxV0Dc2rw",
                "category" : "Post-Game",
                "team" : ["Rod Patrón"],
                "date" : "January 21st 2021"
            },
            {
                "url" : "https://www.youtube.com/watch?v=nq0358cJb-M",
                "category" : "Post-Game",
                "team" : ["Rod Patrón"],
                "date" : "January 17th 2021"
            },
            {
                "url" : "https://www.youtube.com/watch?v=iALl-ce5dzY",
                "category" : "Pre-Game",
                "team" : ["Rodholfo Silva"],
                "date" : "January 16th 2021"
            },
            {
                "url" : "https://www.youtube.com/watch?v=UY4WvV3dHNY",
                "category" : "Enterviews",
                "team" : ["Mathew Francis, Rodholfo Silva, Rod Patrón"],
                "date" : "July 16th 2020"
            },
            {
                "url" : "https://www.youtube.com/watch?v=7E2hMsmdbd4",
                "category" : "Post-Game",
                "team" : ["Rod Patrón"],
                "date" : "January 24th 2021"
            }
        ];
    
    const postGames = data.filter(({ category }) => category === "Post-Game").slice(-2);
    console.log(postGames);
    
    /*
    
    const postGames = data
        .filter(({ category }) => category === "Post-Game")
        .filter(({ date }) => //put your condition here)
        .slice(-2);
    console.log(postGames);
    
    const postGamesByDatet = data
        .filter(({category, date }) => category === "Post-Game" && date_is_after(date))
        .slice(-2);
    */

    【讨论】:

    • 太棒了。是否也可以“混合”过滤器并按日期对其进行排序?
    • 如果您想在获取最后两项之前或slice 之后对结果进行排序,则可以在slice 之前通过将sort 函数链接到它来按日期对其进行排序最终结果,与过滤相同,如果您想按日期过滤所有 postGame,例如在 January 20th 之后,您只需将其添加到条件中。过滤器的“混合”是什么意思
    • 混合我的意思是,就像添加多个过滤器。
    • 是的,没有任何问题,您可以通过链接函数或在过滤器中添加新条件来堆叠函数,我已经更新了我的回复
    【解决方案2】:

    假设文件内容包含在records 变量中,而您想要的记录称为result

    const result = records.videos.filter(video => video.category === 'Post-Game').slice(-2);
    

    【讨论】:

    • 太棒了。是否也可以“混合”过滤器并按日期对其进行排序?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多