[英]How to get the properties from object
I am trying to handle a callback data from ajax and having a problem looping the data. 我正在尝试处理来自ajax的回调数据,并且在循环数据时遇到问题。
I have 我有
data.prototype.handleReturnData = function(data) {
}
data
is an object which contains 4 objects. data
是一个包含4个对象的对象。 Each object has a test
and test2
property. 每个对象都有一个
test
和test2
属性。
How do I get those properties? 我如何获得这些财产?
Thanks a lot! 非常感谢!
尝试使用data[0]['test']
访问您的值。
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. 这将获取所有属性名称,您可以将其用于循环浏览或选择您的属性。
You can use a for-in loop: 您可以使用for-in循环:
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 hasOwnProperty
检查的目的是排除您可能不感兴趣的继承属性。一些文档位于: https : //developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Statements/for.。 。在
Try this: 尝试这个:
for (var i = 0; i < data.length; i++) {
alert(data[i].test);
alert(data[i].test2);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.