With the given JSON structure, I am looking to extract a specific attribute and store it in a collection. The data is formatted as follows:
{
foo: "lorem ipsum",
bars: [{
a: "a",
b: {c: "d", e: "f"}
}, {
a: "u",
b: {w: "x", y: "x"}
}]
}
I know how to extract only bars
(excluding foo bars
) with the help of parse, but my goal now is to retrieve bars
, locate a specific object based on the a
attribute, and then store b
inside a collection.
The approach I have in mind involves:
MyCollection = Backbone.Collection.extend({
model: MyModel,
url: "someUrl",
parse: function(data) {
data.bars.each(function(bar) {
if (bar.a == i) {
return bar.b;
}
}
}
};
var myCollection = new MyCollection();
myCollection.fetch({
success: function() {
console.log("Successfully collected the correct 'b' values!");
}
});
My challenge lies in determining where and how to input the value of i
for the condition if(bar.a == i)
.