1

So i have a javascript function. It performs one ajax call, to retrieve a json object full of data. Within the success function of that ajax call i perfrom ANOTHER ajax call to retrieve the names of the columns of data for that particular set. (yes i'm sure there's a better way but thats not my issue). At this point in time i have two variables: items(json array) and interfaceCols(just an array of strings). Then i try to create an html string to create a table with said data.

Here is my code which will make everything more clear:

$.ajax({
        url:"/ryan/nonEmber/ajax.php?table=Interfaces",
                    beforeSend: function(XMLHttpRequest){},
                    success: function(data, textStatus) {
                    interfaceCols = data.split(" ");
                    $.getJSON("/ryan/nonEmber/getJson.php?table=Interfaces", function( data ){
                        var items = [];
                         $.each(data.post, function(key, val){
                            items.push(val);
                          });
                        for(i = 0; i < items.length; i++){
                            var myString = '<tr id = "visibleRow">';
                            console.log(items[i].interfaceCols[4]);
                            for(j = 0; j < interfaceCols.length; j++){                              
                                myString = myString + '<td id = "visibleDef">' + items[i].interfaceCols[j] +'</td>';
                            }
                            myString = myString + '</tr>';
                            interfaces.push(myString);
                        }

                     });                    
                     }
    });

My javascript file throws an error on the "myString = myString + '<td id ='... line.

I am almost positive that it is because i'm just placing a string at the end of "items[i]" with ".interfaceCols[j]"

Can anybody suggest a way to do this? The whole point is so that i dont have to manually type out every column name, because i have alot of tables and each table has many columns.

3
  • 2
    What is the error? What does the JSON look like? Commented Aug 6, 2014 at 21:57
  • Is post an Object property of your data Object, and an Array, returned in $.getJSON('url.json', function(here){/* use here */});? Commented Aug 6, 2014 at 22:05
  • console.log(items) after the $.each() loop. Commented Aug 6, 2014 at 22:10

1 Answer 1

3

Given a JS object s:

s = {a: 1, b: 2}

You have (at least) two options to access an attribute:

s.a // returns 1

Which seems to be what you are trying to do right now. To access it with a dynamic name, you can use:

s['a'] // returns 1

In your case, it should be:

items[i][interfaceCols[4]]
Sign up to request clarification or add additional context in comments.

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.