0

I was wondering if it's possible to request data from a JSON file (For example: customers.name). But instead of that using an array containing the JSON object names and looping it. My code is below.

function load(url ,callback) {
    var xobj = new XMLHttpRequest();
    xobj.overrideMimeType("application/json");
    xobj.open('GET', url, true);
    xobj.onreadystatechange = function () {
        if (xobj.readyState == 4 && xobj.status == 200) {
            callback(xobj.responseText);
        }
    };
    xobj.send(null);
}

load("klanten.json", function(response) {
    var klanten = JSON.parse(response);
    //Array containing JSON file object names.
    var infArray = ['name', "address", "email", "phone", "place", "zip"];
    //Calling said info using a for loop.
    for(var i = 0; i < infArray.length; i++) {
      console.log(klanten[i].infArray[i]);
      //It not working for some reason.
    }
});

I`d love some help with this. And in case what im asking is completely stupid, also let me know! Any help is welcome, thanks!

1
  • change; klanten[i].infArray[i] to klanten[i][infArray[i]] Commented Mar 7, 2019 at 15:32

1 Answer 1

2

Change console.log(klanten[i].infArray[i]); to:

console.log(klanten[i][infArray[i]]);
Sign up to request clarification or add additional context in comments.

3 Comments

It is more helpful if you describe what it is (from this to that).
and also, explain why this code would work better (why did you make this change, etc.)
Thanks this worked. Im a moron. Thanks alot! I`ll accept your answer in 10 min :).

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.