0

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.

DiegoKTC
  • 9
  • 5
  • To be sure i understood it right: Your model attribute is an array and you want to filter the whole collection based on a value in this array? So not filter it by the models attribute but by the "models attribute attribute"? – homtg Nov 13 '13 at 17:12
  • I have already generated a collection from an attribute. So now when i make collectionGenerated.where(name: 'attribute'); Now i want to filter based on a sectionArray attribute i have from the filtered collection. Something like: collectionGenerated.where(name: 'attribute').sections.where(itemId: 'attribute'); – DiegoKTC Nov 13 '13 at 17:15

1 Answers1

0

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';
});
homtg
  • 1,999
  • 1
  • 15
  • 20
  • You were right, filtering the array was the answer. The only thing i have modified about your code was this: var result = $.grep(arr[0].attributes.sections, function(val) { return val.itemId === 'inicio'; }); Thank you. – DiegoKTC Nov 13 '13 at 17:57