访问“models"javascript对象的

accessing "models" of a javascript object

本文关键字:javascript 对象 quot models 访问      更新时间:2023-09-26

我有一个对象,看起来像这样在我的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
我应该在课堂上专心听讲的。但是我如何访问"collection: Array[20]"呢?有办法吗?
// Get array
r.models[0].attributes.collection

应该让您找到属于models数组中第一个模型的集合

如果你想在数组中单独一个元素:-

// Get first element of collection on first model
r.models[0].attributes.collection[0]

它看起来像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')