0

I am trying to handle a callback data from ajax and having a problem looping the data.

I have

data.prototype.handleReturnData = function(data) {

}

data is an object which contains 4 objects. Each object has a test and test2 property.

How do I get those properties?

Thanks a lot!

0

4 Answers 4

2

Try accessing your values with data[0]['test'].

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a for-in loop:

for (var prop in data) {
    if( data.hasOwnProperty(prop)) {
        // 'prop' refers to the property name
        // do something with data[prop] or data[prop].test
    }
}

The purpose of the hasOwnProperty check is to exclude inherited properties, which you probably aren't interested in. Some documentation here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in

Comments

1

I would suggest:

Object.getOwnPropertyNames(yourobject);

This will get all the property names which you can then use to cycle through or pick your property.

Comments

0

Try this:

for (var i = 0; i < data.length; i++) {
    alert(data[i].test);
    alert(data[i].test2);
}

2 Comments

Not quite right. I can alert(data[i].test) and it alert the code of the function. If we wanted only properties or did not know the names of said properties we could not do this.
@ars265 Yes but that is not what the question asks for. If it asked for getting the prop values without knowing the props, then I would have given a different answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.