主干集合过滤器与位置和获取对象数组

Backbone collection filter with where and get array of objects

本文关键字:获取 取对象 数组 位置 集合 过滤器      更新时间:2023-09-26

我有一个这样的json:

Constants
Contents(Array)
->Sections(Array)
-->sections.attribute1
->constants.attribute1
->constants.attribute2
Templates

我已经过滤了 json 以获取具有以下代码的内容集合:

var viasModel = Backbone.Model.extend({});
var viasCollection = Backbone.Collection.extend({
    model: viasModel,
    url: TEMPLATES_SERVICE,
    parse: function(data){
        return data.contents.data;
    },
    initialize: function(){
        this.fetch();
    },
    route: function(via){
        return this.where({viaId: via});
    }
});

假设我已经使用路由函数过滤了集合的位置。

如何过滤部分(数组)并仅获取过滤集合的部分的元素?

是否与路由的每个返回值有关?

this.where({viaId: via});

我必须找到如何嵌套集合吗?任何帮助将不胜感激。

--编辑--

当我将变量分配给 arr 时,我得到一个带有属性的对象。

attributes: Object
-name: 'test',
-sections: Array[9]
--0: Object
---itemId: 'inicio'
---content: Array[2]
--1: Object
---itemId: 'hola'
---content: Array[2]

我想获取 itemId === 'inicio' 部分数组的内容对象。

不想看起来很糟糕,所以如果你能指导我或给我一些帮助,我没事。

谢谢。

好的,感谢您的评论,似乎这基本上是关于过滤数组的,这是我的建议:

var arr = collectionGenerated.where(name: 'attribute');
var result = $.grep(arr, function(val) {
   return val.itemId == 'attribute';
});

结果是仅包含适合两个过滤器的数组。

--编辑--

我想在你的数据结构中它是

var result = $.grep(arr.sections, function(val) {
 return val.itemId === 'inicio';
});