Home > database >  Currently learning svelte and need some assistance
Currently learning svelte and need some assistance

Time:05-24

Currently I am trying to create a hacker news clone (not from the example given on website). Currently I made an api call that returns an array but I can't seem to get rid of the square brackets. For reference my code is below

onMount(() => {
    fetch('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty&limitToFirst=10&orderBy="$key"')
        .then((res) => {
            return res.text();
        })
        .then((text) => {
            items = text.split(",");
            setTimeout(3000);
            data = items.filter((val) => {
                return val.replace(/[\[\]'] /g, "");
            });
        });
    //console.log(data);
    //getData(items).then(console.log);
});

Thanks in advance!

CodePudding user response:

The API provides a JSON object, but you read it as text (res.text()). Replace this with res.json() and the result will automatically be parsed to an array of IDs.

There is no need to manipulate JSON in string form, just parse it/let it be parsed.

  • Related