无法访问数组中对象的属性

时间:2014-05-22 20:41:34

标签: javascript object for-loop parse-platform

我目前有一个名为结果的对象数组存储在Parse数据库中。我一直在尝试访问结果数组中每个对象的属性。

我有一个遍历结果数组的每个元素的for循环,以及另一个循环内部循环并从每个对象中检索特定属性。在这种情况下,属性是maxPrice,我正在尝试检索存储在数据库中的整数。当我运行这个For循环时,它返回一个长列表,可以在底部看到,而不是我正在寻找的数字。

for (var i = 0; i < results.length; i++)     
{
     var stuff;
     var object = results[i];
     for (var maxPrice in object) //max price is the property
     {
          stuff = object[maxPrice];
          console.log(maxPrice, stuff);
      }
  }

日志:

    I2014-05-22T20:36:17.589Z] _serverData
I2014-05-22T20:36:17.589Z] _opSetQueue
I2014-05-22T20:36:17.589Z] attributes
I2014-05-22T20:36:17.589Z] _hashedJSON
I2014-05-22T20:36:17.589Z] _escapedAttributes
I2014-05-22T20:36:17.589Z] cid
I2014-05-22T20:36:17.589Z] changed
I2014-05-22T20:36:17.589Z] _silent
I2014-05-22T20:36:17.589Z] _pending
I2014-05-22T20:36:17.589Z] _hasData
I2014-05-22T20:36:17.589Z] _previousAttributes
I2014-05-22T20:36:17.589Z] id
I2014-05-22T20:36:17.589Z] createdAt
I2014-05-22T20:36:17.589Z] updatedAt
I2014-05-22T20:36:17.589Z] className
I2014-05-22T20:36:17.589Z] constructor
I2014-05-22T20:36:17.589Z] on
I2014-05-22T20:36:17.589Z] off
I2014-05-22T20:36:17.589Z] trigger
I2014-05-22T20:36:17.589Z] bind
I2014-05-22T20:36:17.589Z] unbind
I2014-05-22T20:36:17.589Z] _existed
I2014-05-22T20:36:17.589Z] initialize
I2014-05-22T20:36:17.589Z] toJSON
I2014-05-22T20:36:17.589Z] _toFullJSON
I2014-05-22T20:36:17.589Z] _refreshCache
I2014-05-22T20:36:17.589Z] dirty
I2014-05-22T20:36:17.589Z] dirtyKeys
I2014-05-22T20:36:17.589Z] _toPointer
I2014-05-22T20:36:17.589Z] get
I2014-05-22T20:36:17.589Z] relation
I2014-05-22T20:36:17.589Z] escape
I2014-05-22T20:36:17.589Z] has
I2014-05-22T20:36:17.589Z] _mergeMagicFields
I2014-05-22T20:36:17.589Z] _copyServerData
I2014-05-22T20:36:17.589Z] _mergeFromObject
I2014-05-22T20:36:17.589Z] _startSave
I2014-05-22T20:36:17.590Z] _cancelSave
I2014-05-22T20:36:17.590Z] _finishSave
I2014-05-22T20:36:17.590Z] _finishFetch
I2014-05-22T20:36:17.590Z] _applyOpSet
I2014-05-22T20:36:17.590Z] _resetCacheForKey
I2014-05-22T20:36:17.590Z] _rebuildEstimatedDataForKey
I2014-05-22T20:36:17.590Z] _rebuildAllEstimatedData
I2014-05-22T20:36:17.590Z] set
I2014-05-22T20:36:17.590Z] unset
I2014-05-22T20:36:17.590Z] increment
I2014-05-22T20:36:17.590Z] add
I2014-05-22T20:36:17.590Z] addUnique
I2014-05-22T20:36:17.590Z] remove
I2014-05-22T20:36:17.590Z] op
I2014-05-22T20:36:17.590Z] clear
I2014-05-22T20:36:17.590Z] _getSaveJSON
I2014-05-22T20:36:17.590Z] _canBeSerialized
I2014-05-22T20:36:17.590Z] fetch
I2014-05-22T20:36:17.590Z] save
I2014-05-22T20:36:17.590Z] destroy
I2014-05-22T20:36:17.590Z] parse
I2014-05-22T20:36:17.590Z] clone
I2014-05-22T20:36:17.590Z] isNew
I2014-05-22T20:36:17.590Z] change
I2014-05-22T20:36:17.590Z] existed
I2014-05-22T20:36:17.590Z] hasChanged
I2014-05-22T20:36:17.590Z] changedAttributes
I2014-05-22T20:36:17.590Z] previous
I2014-05-22T20:36:17.590Z] previousAttributes
I2014-05-22T20:36:17.590Z] isValid
I2014-05-22T20:36:17.590Z] validate
I2014-05-22T20:36:17.590Z] _validate
I2014-05-22T20:36:17.590Z] getACL
I2014-05-22T20:36:17.590Z] setACL

2 个答案:

答案 0 :(得分:1)

看起来results是一个对象数组,但这些对象不是您期望的类型(可能是Parse的内部结构)。通过快速查看Parse文档,我的猜测是你需要做类似的事情:

results[i].get("fieldName")

访问返回对象的值。这意味着您需要提前知道要访问的字段名称。

答案 1 :(得分:0)

或者,您也可以在parse对象上使用toJSON()方法来检索数据的json对象。

results[i].toJSON();