Question

I have a json like this:

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

I have filtered the json to get a collection of Contents with this code:

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});
    }
});

Lets say i have filtered with the route function with where the collection.

How can i filter the Sections(Array) and get only the elements of the sections of the filtered collection?

Is it with a each of the returned value of route?

this.where({viaId: via});

Do i have to find how can i nest a collection? Any help will be appreciated.

--EDIT--

When I assign the variable to arr, i get an object with attributes.

attributes: Object

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

--1: Object
---itemId: 'hola'
---content: Array[2]

I want to get the content Object of the itemId === 'inicio' of the sections Array.

I don't want to look bad, so if you could guide me or give me some help I'm ok.

Thank you.

Was it helpful?

Solution

Okay thanks for your comment, seems like this is basically about filtering an array, here is my proposal:

var arr = collectionGenerated.where(name: 'attribute');

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

result being the array containing only what fits both filters.

-- EDIT --

i guess in your data structure it is

var result = $.grep(arr.sections, function(val) {
 return val.itemId === 'inicio';
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top