Home > Net >  Using json.prase to seprating each column javascript
Using json.prase to seprating each column javascript

Time:12-24

on javascript im coding a scraper for my and for my search is in json and i wanna seprate each column that have value and data but i tried so many methods it turns out like this

    searchMangaFromElement(element) {
        var obj = JSON.parse(element.text())
        var name = obj.suggestions.map(user => user.value);
        var url = obj.suggestions.map(user => user.data);
        var thumbnail = `${this.baseUrl}/uploads/manga/${url}/cover/cover_250x350.jpg`
        var rank = '0';
        return super.manga(name,url,thumbnail,rank);
    }
{
  "mangas": [
    {
      "name": [
        "The Boxer",
        "I'm the Grim Reaper",
        "The Girl Downstairs",
        "The Beginning After the End",
        "The God of High School",
        "The Gamer",
        "The Red King",
        "Boyfriend of the Dead",
        "The Tutorial is Too Hard",
        "Mook-Hyang The Origin",
        "I'm a Martial Art Villainess, but I'm the Strongest!",
        "The Tutorial Tower of the Advanced Player",
        "My Level's the Best",
        "The Throne",
        "The Legendary Mechanic",
        "The Newlywed Life of a Witch and a Dragon",
        "The Player that can't Level Up",
        "Return of the Frozen Player",
        "Capture the Golem and Escape Poverty",
        "The Thorn That Pierces Me",
        "The First Night With the Duke",
        "A Way to Protect the Lovable You",
        "Return of the Female Knight",
        "The Strongest Florist",
        "Return of the Mount Hua Sect",
        "The Villainess is a Marionette",
        "The Princess Imprints a Traitor",
        "Overnight With The Emperor",
        "Kill the Villainess",
        "The Constellation That Returned From Hell",
        "The Pampered Regent of the Richest Woman",
        "The Four of Them",
        "The Kiss Bet",
        "The Lord’s Coins Aren’t Decreasing?!",
        "The Sword Of Dawn",
        "The Hip Guy",
        "Rise From The Rubble",
        "The Supreme Dantian",
        "The Remarried Empress",
        "Reformation of the Deadbeat Noble",
        "The Build Up",
        "Taming The Lady",
        "I Became the Tyrant’s Secretary",
        "Men of the Harem",
        "How The Knight Lives As A Lady",
        "So I Married the Anti-Fan",
        "The Game That I Came From",
        "The Heavenly Demon Can’t Live a Normal Life",
        "The Dexter Attack",
        "Nevertheless",
        "Reincarnation of the Suicidal Battle God",
        "The Makeup Remover",
        "Nevertheless, I love you.",
        "My Path to Killing Gods in Another World",
        "The Blade of Evolution Walking Alone In The Dungeon",
        "The Strongest Warrior Goes to School",
        "The King Of Bugs",
        "The Dark Magician Transmigrates After 66666 Years",
        "Return of the 8th class Magician",
        "The Max Level Hero has Returned!",
        "The Supreme Dantian",
        "The Book of Lagier",
        "The Ancient Sovereign of Eternity",
        "The Heavenly Demon Destroys the Lich King’s Murim",
        "Throne of the Dragon King",
        "There Must Be Happy Endings",
        "The Return of the Crazy Demon",
        "I Reincarnated As The Crazed Heir",
        "Tricked into Becoming the Heroine's Stepmother",
        "I Become a Doting Father",
        "Long Way of the Warrior",
        "Record of the Mightiest Lord",
        "The Earth Savior Selection",
        "Leveling With The Gods",
        "The Double Agent",
        "I’m the Only One Loved by the Constellations!",
        "The Blood of the Butterfly",
        "Don’t Get Me Wrong, I’m The Real Victim!",
        "My Younger Brother's Friend",
        "The Witch and The Bull",
        "The Male Lead's Girl Friend",
        "This is the Law",
        "The Fantasie of a Stepmother",
        "The Lone Necromancer",
        "The Challenger",
        "The Great Mage that Returned after 4000 Years",
        "The Tomb of Famed Swords",
        "Protect the Knight",
        "Foreigner on the Periphery",
        "Return of the Legendary Spear Knight",
        "Return of the Legend",
        "The S-Classes That I Raised",
        "The Time of the Terminally ill Extra",
        "Return of the Broken Constellation",
        "I Am the Fated Villain",
        "The Frenzy Of Evolution",
        "The Strongest Player"
      ],
      "url": [
        "th3-b0xer",
        "7854-im-the-grim-reaper",
        "th3-girl-d0wnstairs",
        "th3-beginning-aft3r-the-end",
        "the-g0d-of-high-sch00l",
        "th3-gam3r",
        "the-red-king",
        "414-boyfriend-of-the-dead",
        "2-the-tutorial-is-too-hard",
        "4742-the-origin",
        "im-a-martial-art-villainess-but-im-the-strongest",
        "85-the-tutorial-tower-of-the-advanced-player",
        "12-my-levels-the-best",
        "74-the-thr0ne",
        "54-the-legendary-mechanic",
        "th3-newlywed-life-of-a-witch-and-a-drag0n",
        "the-player-that-cant-level-up",
        "0-return-of-the-frozen-player",
        "capture-the-golem-and-escape-poverty",
        "44-the-th0rn-that-pierces-me",
        "th3-first-night-with-the-duk3",
        "474-a-way-to-protect-the-lovable-you",
        "44-return-of-the-female-knight",
        "477-the-strongest-fl0rist",
        "return-0f-the-mount-hua-sect",
        "554-the-villainess-is-a-mari0nette",
        "the-princess-imprints-a-traitor",
        "0vernight-with-the-emper0r",
        "85-kill-the-villainess",
        "the-c0nstellation-that-returned-from-hell",
        "the-pampered-regent-of-the-richest-woman",
        "th3-f0ur-0f-them",
        "th3-kiss-b3t",
        "the-lords-coins-arent-decreasing",
        "the-sword-of-dawn",
        "558-the-hip-guy",
        "rise-from-the-rubble",
        "747-the-supreme-dantian",
        "44-th3-remarried-empress",
        "98-reformation-of-the-deadbeat-noble",
        "th3-build-up4",
        "taming-the-lady",
        "i-became-the-tyrants-secretary",
        "men-of-the-harem",
        "how-the-knight-lives-as-a-lady",
        "87-so-i-married-the-antifan",
        "the-game-that-i-came-from",
        "the-heavenly-demon-cant-live-a-normal-life",
        "the-dexter-attack",
        "n3verthel3ss",
        "reincarnation-of-the-suicidal-battle-god",
        "the-makeup-remover",
        "nevertheless-i-love-you",
        "my-path-to-killing-gods-in-another-world",
        "the-blade-of-evolution-walking-alone-in-the-dungeon",
        "the-strongest-warrior-goes-to-school",
        "the-king-of-bugs",
        "the-dark-magician-transmigrates-after-66666-years",
        "return-of-the-8th-class-magician",
        "the-max-level-hero-has-returned",
        "the-supreme-dantian",
        "the-book-of-lagier",
        "the-ancient-sovereign-of-eternity",
        "the-heavenly-demon-destroys-the-lich-kings-murim",
        "throne-of-the-dragon-king",
        "there-must-be-happy-endings",
        "the-return-of-the-crazy-demon",
        "i-reincarnated-as-the-crazed-heir",
        "tricked-into-becoming-the-heroines-stepmother",
        "i-become-a-doting-father",
        "long-way-of-the-warrior",
        "record-of-the-mightiest-lord",
        "the-earth-savior-selection",
        "leveling-with-the-gods",
        "the-double-agent",
        "im-the-only-one-loved-by-the-constellations",
        "the-blood-of-the-butterfly",
        "dont-get-me-wrong-im-the-real-victim",
        "my-younger-brothers-friend",
        "the-witch-and-the-bull",
        "the-male-leads-girl-friend",
        "this-is-the-law",
        "the-fantasie-of-a-stepmother",
        "the-lone-necromancer",
        "the-challenger",
        "the-great-mage-that-returned-after-4000-years",
        "the-tomb-of-famed-swords",
        "protect-the-knight",
        "foreigner-on-the-periphery",
        "return-of-the-legendary-spear-knight",
        "return-of-the-legend",
        "the-sclasses-that-i-raised",
        "the-time-of-the-terminally-ill-extra",
        "return-of-the-broken-constellation",
        "i-am-the-fated-villain",
        "the-frenzy-of-evolution",
        "the-strongest-player"
      ],
      "thumbnail": "https://zahard.xyz/uploads/manga/th3-b0xer,7854-im-the-grim-reaper,th3-girl-d0wnstairs,th3-beginning-aft3r-the-end,the-g0d-of-high-sch00l,th3-gam3r,the-red-king,414-boyfriend-of-the-dead,2-the-tutorial-is-too-hard,4742-the-origin,im-a-martial-art-villainess-but-im-the-strongest,85-the-tutorial-tower-of-the-advanced-player,12-my-levels-the-best,74-the-thr0ne,54-the-legendary-mechanic,th3-newlywed-life-of-a-witch-and-a-drag0n,the-player-that-cant-level-up,0-return-of-the-frozen-player,capture-the-golem-and-escape-poverty,44-the-th0rn-that-pierces-me,th3-first-night-with-the-duk3,474-a-way-to-protect-the-lovable-you,44-return-of-the-female-knight,477-the-strongest-fl0rist,return-0f-the-mount-hua-sect,554-the-villainess-is-a-mari0nette,the-princess-imprints-a-traitor,0vernight-with-the-emper0r,85-kill-the-villainess,the-c0nstellation-that-returned-from-hell,the-pampered-regent-of-the-richest-woman,th3-f0ur-0f-them,th3-kiss-b3t,the-lords-coins-arent-decreasing,the-sword-of-dawn,558-the-hip-guy,rise-from-the-rubble,747-the-supreme-dantian,44-th3-remarried-empress,98-reformation-of-the-deadbeat-noble,th3-build-up4,taming-the-lady,i-became-the-tyrants-secretary,men-of-the-harem,how-the-knight-lives-as-a-lady,87-so-i-married-the-antifan,the-game-that-i-came-from,the-heavenly-demon-cant-live-a-normal-life,the-dexter-attack,n3verthel3ss,reincarnation-of-the-suicidal-battle-god,the-makeup-remover,nevertheless-i-love-you,my-path-to-killing-gods-in-another-world,the-blade-of-evolution-walking-alone-in-the-dungeon,the-strongest-warrior-goes-to-school,the-king-of-bugs,the-dark-magician-transmigrates-after-66666-years,return-of-the-8th-class-magician,the-max-level-hero-has-returned,the-supreme-dantian,the-book-of-lagier,the-ancient-sovereign-of-eternity,the-heavenly-demon-destroys-the-lich-kings-murim,throne-of-the-dragon-king,there-must-be-happy-endings,the-return-of-the-crazy-demon,i-reincarnated-as-the-crazed-heir,tricked-into-becoming-the-heroines-stepmother,i-become-a-doting-father,long-way-of-the-warrior,record-of-the-mightiest-lord,the-earth-savior-selection,leveling-with-the-gods,the-double-agent,im-the-only-one-loved-by-the-constellations,the-blood-of-the-butterfly,dont-get-me-wrong-im-the-real-victim,my-younger-brothers-friend,the-witch-and-the-bull,the-male-leads-girl-friend,this-is-the-law,the-fantasie-of-a-stepmother,the-lone-necromancer,the-challenger,the-great-mage-that-returned-after-4000-years,the-tomb-of-famed-swords,protect-the-knight,foreigner-on-the-periphery,return-of-the-legendary-spear-knight,return-of-the-legend,the-sclasses-that-i-raised,the-time-of-the-terminally-ill-extra,return-of-the-broken-constellation,i-am-the-fated-villain,the-frenzy-of-evolution,the-strongest-player/cover/cover_250x350.jpg",
      "rank": "0"
    }
  ],
  "hasNextPage": 1,
  "nextPage": 1,
  "results": 1
}

I tried it doing it like that same thing but now prints one thing from it

    searchMangaFromElement(element) {
        var obj = JSON.parse(element.text())
        for (var vals in obj. suggestions) {
        var url = obj.suggestions[vals].data;
        var name = obj.suggestions[vals].value;
        var thumbnail = `${this.baseUrl}/uploads/manga/${url}/cover/cover_250x350.jpg`
        var rank = '0';
        return super.manga (name, url, thumbnail,rank);
        }
    }
{
  "mangas": [
    {
      "name": "The Boxer",
      "url": "th3-b0xer",
      "thumbnail": "https://zahard.xyz/uploads/manga/th3-b0xer/cover/cover_250x350.jpg",
      "rank": "0"
    }
  ],
  "hasNextPage": 1,
  "nextPage": 1,
  "results": 1
}

the default was using cheerio to find specific things and it prints like that

{
  "mangas": [
    {
      "name": "10 Years in the Friend Zone",
      "url": "/manga/2053/10-years-in-the-friend-zone",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5faa0b5f66d2cb714b2b2df9.png?1640316529137?",
      "rank": "0"
    },
    {
      "name": "10th - You and I Fell in Love With the Same Person.",
      "url": "/manga/2107/10th-you-and-i-fell-in-love-with-the-same-person",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5fab17b842e2937121a252de.jpg?1640316299796?",
      "rank": "0"
    },
    {
      "name": "11 Eyes - Tsumi to Batsu to Aganai no Shoujo",
      "url": "/manga/1517/11-eyes-tsumi-to-batsu-to-aganai-no-shoujo",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5f97927ab5ef1960d7b17505.jpg?1640315012618?",
      "rank": "0"
    },
    {
      "name": "25-ji no Ghost Writer",
      "url": "/manga/2153/25-ji-no-ghost-writer",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5fab6a8925d77b716a370f7b.jpg?1640316101771?",
      "rank": "0"
    },
    {
      "name": "2ban-me no Alpha",
      "url": "/manga/2343/2ban-me-no-alpha",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5fe26d358d9b52437f740e69.jpg?1640316826822?",
      "rank": "0"
    },
    {
      "name": "30-sai, Shojo OL",
      "url": "/manga/852/30-sai-shojo-ol",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5f7a5fbb9c50b229a3c2a156.jpg?1640308964416?",
      "rank": "0"
    },
    {
      "name": "5Toubun no Hanayome",
      "url": "/manga/739/5toubun-no-hanayome",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5f78e1e428b7864469d947b7.jpg?1640316701098?",
      "rank": "0"
    },
    {
      "name": "666 Satan",
      "url": "/manga/934/666-satan",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5f7b9fb2fd59c26384219b17.jpg?1640315727785?",
      "rank": "0"
    },
    {
      "name": "A Deal With the Devil",
      "url": "/manga/2351/a-deal-with-the-devil",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5fe45e578264bc2beea65a16.jpg?1640312615522?",
      "rank": "0"
    },
    {
      "name": "A Maid With Special Circumstances and the Young Miss Who Wants to Get Along",
      "url": "/manga/1903/a-maid-with-special-circumstances-and-the-young-miss-who-wants-to-get-along",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5fa600806d472b71ce0aebd5.jpg?1640305967164?",
      "rank": "0"
    },
    {
      "name": "A Story About Confessing Before the End of the World",
      "url": "/manga/992/a-story-about-confessing-before-the-end-of-the-world",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5f7bdefefd59c2638421bb15.jpg?1640316238461?",
      "rank": "0"
    },
    {
      "name": "A Story About a Grandpa and Grandma Who Returned Back to Their Youth",
      "url": "/manga/1686/a-story-about-a-grandpa-and-grandma-who-returned-back-to-their-youth",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5fa0b0ae468a6c1ea40b221c.jpg?1640307533358?",
      "rank": "0"
    },
    {
      "name": "A Tender Heart: The Story of How I Became a Duke's Maid",
      "url": "/manga/2477/a-tender-heart-the-story-of-how-i-became-a-duke-s-maid",
      "thumbnail": "https://cdn.mangaworld.in/mangas/6086262598a5c33b63211285.jpg?1640316531952?",
      "rank": "0"
    },
    {
      "name": "A Trace of You",
      "url": "/manga/2021/a-trace-of-you",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5fa9c8d46af12c711b4702a7.jpg?1640314754722?",
      "rank": "0"
    },
    {
      "name": "Adamasu no Majotachi",
      "url": "/manga/1642/adamasu-no-majotachi",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5f9cc2de7b46fd1e2d722356.jpg?1640316533446?",
      "rank": "0"
    },
    {
      "name": "Ageha o Ou Monotachi",
      "url": "/manga/551/ageha-o-ou-monotachi",
      "thumbnail": "https://cdn.mangaworld.in/mangas/5f7663ba191ebd03ee7f23b9.jpg?1640315024255?",
      "rank": "0"
    }
  ],
  "hasNextPage": true,
  "nextPage": 2,
  "results": 16
}

CodePudding user response:

Try this code

searchMangaFromElement(element) {
    const obj = JSON.parse(element.text())
    const allMangas = [] // define an array
    for (var vals in obj. suggestions) {
        var url = obj.suggestions[vals].data;
        var name = obj.suggestions[vals].value;
        var thumbnail = `${this.baseUrl}/uploads/manga/${url}/cover/cover_250x350.jpg`
        var rank = '0';
        // push each item into new array
        allMangas.push({name, url, thumbnail,rank})
    }

    console.log('allMangas:', allMangas)
    
    // and assign it to super.manga
    return super.manga(allMangas);
}

If it doesn't work, show me what super.manga does

  • Related