Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

Query category via JSON with Javascript

I have this JSON file. I would like to query only the Post-Game categories, however, I need the last two of them.

    {
    "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"
        }
    ]
}

I am using Javascript, via for loops and slice(1).slice(-2), but not success.

question from:https://stackoverflow.com/questions/65910849/query-category-via-json-with-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
*/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...