javascriptbackbone.jsunderscore.jsbackbone.js-collections

set attribute of all models in backbone collection


I understand that using pluck method we can get an array of attributes of each model inside a backbone collection

var idsInCollection = collection.pluck('id'); // outputs ["id1","id2"...]

I want to if there is a method that sets up an attribute to each model in the collection,

var urlArray = ["https://url1", "https://url1" ...];
collection.WHAT_IS_THIS_METHOD({"urls": urlArray});

Solution

  • I don’t there is a method for it, but you can try:

    collection.forEach(function(model, index) {
        model.set(url, urlArray[index]);
    });