如何遍历一个对象的所有字段
How to iterate through all the fields of an object
我有一个大约有 23 列的对象。有没有办法自动遍历每一列?而不是使用 .get("COLUMN_NAME")
?
专门选择每一列
谢谢大家。
也就是说 Class
A
-- 有 fields'
id
, createdAt
, updatedAt
, a
, b
、c
和 obj
是 A
的实例。
obj.attributes
是一个包含 a
、b
、c
和 id
、createdAt
、updateAt
的对象是 obj
.
的属性
以下是在 web 控制台中显示除特殊字段 (id
、createdAt
、updatedAt
) 之外的所有字段名称的示例。
Object.keys(obj.attributes).forEach(function(fieldName) {
console.log(fieldName);
});
更简单:
object.get('COLUMN_NAME')
等同于 object.attributes.COLUMN_NAME
因此,如果您执行 console.log(object.attributes)
,您将得到一个 JS 对象作为示例:
{
"cheatMode":true
"createdAt":Tue Oct 30 2018 10:57:08 GMT+0100 (heure normale d’Europe centrale) {} (this is a JS Date object)
"playerName":"Sean Plott"
"score":1337
"updatedAt":Tue Oct 30 2018 12:18:18 GMT+0100 (heure normale d’Europe centrale) {} (this is a JS Date object)
}
具有所有属性及其值。
就这些了。
ParseServer 查询的完整示例代码
const GameScore = Parse.Object.extend("GameScore");
const mainQuery = new Parse.Query(GameScore);
mainQuery.equalTo("cheatMode", true);
mainQuery.find().then(async (response) => {
response.map(function(object){
console.log(object.attributes)
// Will log for example :
// {
// "cheatMode":true
// "createdAt":Tue Oct 30 2018 10:57:08 GMT+0100 (heure normale d’Europe centrale) {} (this is a JS Date object)
// "playerName":"Sean Plott"
// "score":1337
// "updatedAt":Tue Oct 30 2018 12:18:18 GMT+0100 (heure normale d’Europe centrale) {} (this is a JS Date object)
// }
})
});
我有一个大约有 23 列的对象。有没有办法自动遍历每一列?而不是使用 .get("COLUMN_NAME")
?
谢谢大家。
也就是说 Class
A
-- 有 fields'
id
, createdAt
, updatedAt
, a
, b
、c
和 obj
是 A
的实例。
obj.attributes
是一个包含 a
、b
、c
和 id
、createdAt
、updateAt
的对象是 obj
.
以下是在 web 控制台中显示除特殊字段 (id
、createdAt
、updatedAt
) 之外的所有字段名称的示例。
Object.keys(obj.attributes).forEach(function(fieldName) {
console.log(fieldName);
});
更简单:
object.get('COLUMN_NAME')
等同于 object.attributes.COLUMN_NAME
因此,如果您执行 console.log(object.attributes)
,您将得到一个 JS 对象作为示例:
{
"cheatMode":true
"createdAt":Tue Oct 30 2018 10:57:08 GMT+0100 (heure normale d’Europe centrale) {} (this is a JS Date object)
"playerName":"Sean Plott"
"score":1337
"updatedAt":Tue Oct 30 2018 12:18:18 GMT+0100 (heure normale d’Europe centrale) {} (this is a JS Date object)
}
具有所有属性及其值。 就这些了。
ParseServer 查询的完整示例代码
const GameScore = Parse.Object.extend("GameScore");
const mainQuery = new Parse.Query(GameScore);
mainQuery.equalTo("cheatMode", true);
mainQuery.find().then(async (response) => {
response.map(function(object){
console.log(object.attributes)
// Will log for example :
// {
// "cheatMode":true
// "createdAt":Tue Oct 30 2018 10:57:08 GMT+0100 (heure normale d’Europe centrale) {} (this is a JS Date object)
// "playerName":"Sean Plott"
// "score":1337
// "updatedAt":Tue Oct 30 2018 12:18:18 GMT+0100 (heure normale d’Europe centrale) {} (this is a JS Date object)
// }
})
});