0

My application compares API data, in my case temperatures. I have written it with a function for each API first like this:

    async function prepData(n) {
    const a = [];
    if ((n == 0)) {
        const json = await firstAPI();
        for (var i = 0; i <= c; i++) {
            a.push(json.shortIntervals[i].temperature.value);
        }
        console.log(a);
        return a;
    } else {
        const json = await secondAPI();
        for (var i = 0; i <= c; i++) {
            a.push(
            json.properties.timeseries[i].data.instant.details.air_temperature,
            );
        }
        console.log(a);
        return a;
    }
    }

This code works successfully but it has a lot of repeating code, therefore I want to change it.

I have successfully merged the fetching functions and call the API url from a array by passing the "n" witch determines witch API I want to access. Now I am left standing with the problem that I can't access the data in the different locations.

The code currently looks like this:

    const dataLocation = ['json.first.api.path', 'json.seccond.api.path']

    async function prepData(n) {
    const a = [];

    const json = await fetching(n);
    for (var i = 0; i <= c; i++) {
        a.push(dataLocation[n]);
    }
    console.log(a);
    return a;

You can see that I now have the datalocation stored in a array, but I can't "execute" it, if I put the "dataLocation[n]" in directly I add the path as a string to the new array, and with quotes I push the arrayname.

How can I tell Javascript that it should look in the location defined in the array?

Also: "c" is a number that is defined earlier in the code and defines the amount of data accessed from the api´s

3
  • if your code is actually like this, I'd suggest you don't overcomplicate things. Create a separate function for each API. If there are a few repetitions, that's ok. Commented Dec 9, 2022 at 8:36
  • if ( n=0 ) assigns the value of 0 to n. The result of this assignment is 0 which will evaluate to false- same as if ( 0 ) { or same as if ( false) { , so that block will never get executed. Use if (n === 0) { Commented Dec 9, 2022 at 8:42
  • Related to the comment of @gog I hope that i can avoid that because i feel it is much easyer if i store the location in the array/object. The workaround i currently try is to save all the data i need to the array and then acsess it from there Commented Dec 9, 2022 at 12:04

2 Answers 2

1

In the prepData you are assigning the n value to 0 instead of checking:

  async function prepData(n) {
    const a = [];
    if ((n = 0)) { // **you have error here, please change it to (n == 0)**
        const json = await firstAPI();
        for (var i = 0; i <= c; i++) {
            a.push(json.shortIntervals[i].temperature.value);
        }
        console.log(a);
        return a;
    } else {
        const json = await secondAPI();
        for (var i = 0; i <= c; i++) {
            a.push(
            json.properties.timeseries[i].data.instant.details.air_temperature,
            );
        }
        console.log(a);
        return a;
    }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing that out, also @Dave-Pile. I corrected the mistake in the question
0

You're overcomplicating things.

async function prepData(n) {
  if (n == 0) {
    const response = await firstAPI();
    return response.shortIntervals.map(v => v.temperature.value)
      .slice(0, c + 1); // get rid of this
  }

  const response = await secondAPI();
  return response.properties.timeseries.map(v => v.data.instant.details.air_temperature)
    .slice(0, c + 1); // get rid of this
}

And there is no json in your code, jsons are strings; and names matter.

This variable c is messing with me. However I turn it, it should not be there.

  1. the API should limit the amount of data it returns.
  2. If the API doesn't provide pagination, you should store all the data you get from the API and work/render only a slice of that.
  3. The alternative is that you make (the same) requests to the API over and over again, which send you more data than you need just for you to use only a fraction of it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.