0

I have this code:

<script>
import { onMount } from "svelte";

// Get config
fetch("./config.gb.js", {
    method: "GET",
    headers: {
        "Content-Type": "application/json"
    }
})
.then(res => {
    if (!res.ok) {
        throw new Error("404!");
    }
    return res.json();
})
// Output data in array
.then(data => {
    let keys = Object.values(data);
    console.log(keys);
    console.log(data);
})
// Catch error
.catch(err => {
    console.log(err);
})

and config.gb.js file looks like this:

var countryCurrencyDefaults = {
countries: [{
    country: {
        name: "Afghanistan",
        active: "Y",
        isoCode: "AF",
        dialCode: 93,
        currencies: [{
            currency: "AFN"
        }, {
            currency: "USD"
        }]
    }
}, {
    country: {
        name: "American Samoa",
        active: "Y",
        isoCode: "AS",
        dialCode: 1,
        currencies: [{
            currency: "USD"
        }]
    }
}, { ... other 240+ countries ... }

How to convert data from .js file to object or json array? I'm struggling to find any info on internet that can help me. I'm working with svelte.js but I guess this doesn't matter much.

Jon Levi
  • 51
  • 1
  • 11
  • Why not to "stringify" this `countries` array and save it as a `.json` file? Otherwise, you have to make a server that can respond with this array to the browser. – artanik Mar 15 '20 at 16:33
  • Of course, `.json` also needs to be "responded" from the server. – artanik Mar 15 '20 at 16:35
  • You should [export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) `countryCurrencyDefaults` and [import](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) it in your Svelte file. Also note that judging from your code, you do *not* actually want JSON but just an object ([difference between JSON and the Object Literal Notation](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation)). – str Mar 15 '20 at 16:42
  • @artanik sadly this is not in my will, believe me if I had an option I would convert it to .json, but not in this case – Jon Levi Mar 15 '20 at 16:57
  • @JonLevi yeah, that's bad news, you can require or import it, or make a script tag with this file, this might help as well. – artanik Mar 15 '20 at 17:03
  • 1
    @artanik yeah I know... Anyway thanks for your help – Jon Levi Mar 15 '20 at 17:06
  • @str since you marked question as duplicate and I cannot seem to find an answer in your provided link, can you then provide more details on your question. It's not clear to me and doesn't seem to answer question. – Jon Levi Mar 15 '20 at 18:02
  • Did you also see my comment? `export`/`import` would definitely allow you do access `countryCurrencyDefaults` from within your Svelte code. Can you clarify what you mean by "*sadly this is not in my will*"? – str Mar 15 '20 at 18:45
  • I mean that I cannot edit js file, I'm only allowed to read it. I saw your comment but it's not clear to me. You marked it as duplicate, but question differs. My question is not how to include js into another js but rather how to 'fetch' js to object and I still can't find an answer. – Jon Levi Mar 15 '20 at 18:53

0 Answers0