0

我的 javascript 控制台中有一个如下所示的对象:

r {length: 1, models: Array[1], _byId: Object, constructor: function, model: function…}
    _byId: Object
    length: 1
    models: Array[1]
        0: r
            _changing: false
            _events: Object
            _pending: false
            _previousAttributes: Object
            attributes: Object
                collection: Array[20]
                created_at: Wed Mar 27 2013 03:24:31 GMT-0400 (Eastern Daylight Time)
                __proto__: Object
            changed: Object
            cid: "c26"
            collection: r
            __proto__: s
            length: 1
        __proto__: Array[0]
    __proto__: s

我应该在课堂上注意......但我怎样才能访问那个“集合:数组 [20]”?有办法吗?

4

2 回答 2

1
// Get array
r.models[0].attributes.collection

应该让您进入属于模型数组中第一个模型的集合。

如果您想要数组中的单个元素:-

// Get first element of collection on first model
r.models[0].attributes.collection[0]
于 2013-03-27T07:48:27.610 回答
1

它看起来像backbone.js 集合,因此您可以通过执行以下操作访问该数组:

// Get model by index in collection
var collection = r.at(0).get('collection')

// Or get model by client id (cid)
var collection = r.getByCid('c26').get('collection')
于 2013-03-27T08:03:39.460 回答