-2

I made a function in my JavaScript, here is the scene: Please note, the code below is just for my scenario, its not working.

getData('bill', 'userAge');
Function getData(u, variable) {
    fetch(`url`).then(r=> { return response.json();})
    .then(u => {
        var userFullName    = u.fullname;
        var userAge         = u.age;
        var userGender      = u.gender
        return variable
    });
}

when I execute the function getData('bill', 'userAge') which userAge is the variable name inside the function. And the function will return the value from variable name: userAge inside the function, so I dont have to write another code like, if(variable=='userAge') return userAge;

Is it possible ? Just asking, because I have ton of variables in my function, for now I'm still using if(variable=='userAge') return userAge

Poporingus
  • 41
  • 1
  • 1
  • 4
  • 1
    Well, you can do `return u[variable]`. Although, in that case you need to pass `"age"` not `"userAge"`. – VLAZ Sep 13 '18 at 20:09
  • 3
    This shouldn't run, function should *not* be capitalized – Dexygen Sep 13 '18 at 20:10
  • 1
    instead of creating new `var` for every `u` preperty, why not pass the property name as variable in the parameter, then just do a `return u[variable]`. – Calvin Nunes Sep 13 '18 at 20:10
  • you could return `u[variable]` – Phiter Sep 13 '18 at 20:10
  • 1
    Note that none of the comments will work, since `fetch` is asynchronous... – Heretic Monkey Sep 13 '18 at 20:11
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Heretic Monkey Sep 13 '18 at 20:11
  • But it will be return from the `fetch`, which will be `thenable` value. So he can't just return the value from `Function` like in sync way. – Papi Sep 13 '18 at 20:12
  • 1
    @LucaKiebel but the variable is not attached to `window`? Even if it was, it's polluting the global scope and probably shouldn't be there anyway. – VLAZ Sep 13 '18 at 20:12
  • @Papi The function would have to `return fetch...` for it to be thenable, which it is not currently doing. – Heretic Monkey Sep 13 '18 at 20:13
  • I mean, the code is just an example of my scenario. What im asking for is, can I get the data return from the variable, and call the variable name when I execute the function. Pardon me, my english is very limited, and I believe my question is confusing. – Poporingus Sep 13 '18 at 20:14
  • @HereticMonkey to be honest, there is a lot of things this function is not currently doing...like being a `function`. I take that question as not really being constrained by the example but focusing on the intention. – VLAZ Sep 13 '18 at 20:15
  • @vlaz,luckily that intention is thoroughly met by the duplicate :). – Heretic Monkey Sep 13 '18 at 20:17
  • @HereticMonkey not really? The intention is to return a value dynamically based on a variable. – VLAZ Sep 13 '18 at 20:19
  • @vlaz Meh, that's the (relatively) easy thing to do -- use bracket notation. But knowing about that just gets that value passed from the `then`, so even if the OP fixes that, they're still nowhere near done. – Heretic Monkey Sep 13 '18 at 20:21

2 Answers2

0

One solution:

getData('bill', 'age');
function getData(u, variable) {
    return fetch(`url`).then(r=> { return r.json();})
    .then(u => {
        return u[variable];
    });
}

If you must use different names in your variable parameters than in your u properties, you can map them rather than use if/switch:

paramToVariable = {
    userAge: 'age',
    ...
};
...
        return u[paramToVariable[variable]];

Or you may use a function to map rather than an object.

Will
  • 6,601
  • 3
  • 31
  • 42
  • Hello @Will Cain, Thanks for replying my question.. I did what exactly you wrote there, but perhaps my question is really confusing people who trying to help cause my english is limited to explain what Im really asking for. Let see my scenario here. I have a function above, and I use it in my code `getData('bill', 'userAge')` which userAge is the variable name inside the function, so I dont have to write another code like, `if(variable=='userAge') return userAge;` – Poporingus Sep 13 '18 at 20:22
0

Well, answering directly your question: No, if you want to return exactly with the variable you are passing right now, you'll need to map each variable name to each return property.

BUT, if you pass directly and exactly the name of the property as variable parameter, then you can just use return u[variable]. For Example, instead of passing userFullName, you pass just fullname.

I'm not going to enter in the merit that your current example code being completely wrong because Function ... is not going to work since you should use function ..., but here below I made a functional example using a different approach to the fetch using await and async, you can see that it's returning the property I specified, which is "title".

async function getData(variable) {
    const response = await fetch('https://jsonplaceholder.typicode.com/todos/1', {});
    const u = await response.json();
    return u[variable];
}

getData('title').then(a => {console.log(a)});

To check if my example returned the expected property from the object, you can access the link: JSON Example

Calvin Nunes
  • 6,376
  • 4
  • 20
  • 48
  • Yes, my code above is just the scenario. I just wondering if its possible so I dont have to write other code, cause I currely have all the variables already but without map. Thanks for your time ! – Poporingus Sep 13 '18 at 20:50