Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6546899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:45:53+00:00 2026-05-25T11:45:53+00:00

I have a backbone.js model with an array as a property: defaults: { myArray

  • 0

I have a backbone.js model with an array as a property:

defaults: {
    myArray : [0,1,2]
}

I’m trying to set the value for a particular index.

var myIndex = 1;
myModel.set({"myArray"[myIndex] : newVal}); //doesn't work
myModel.set({"myArray[myIndex]": newVal}); //doesn't work
myModel.set({"myArray" + "[" + myIndex + "]": newVal}); //doesn't work

What’s the proper syntax to get/set array properties? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:45:54+00:00Added an answer on May 25, 2026 at 11:45 am

    the syntax you’re trying doesn’t work because the parameters sent into the set method are an object literal. the values on the left side of the : are treated as literal names, while the values on the right can be executed / interpreted code.

    there’s a few things you can do, though:

    get, update, and set the entire array:

    var a = myModel.get("myArray");
    a[0] = 5
    myModel.set("myArray", a);
    
    myModel.get("myArray"); //=> [5, 1, 2]
    

    the advantage in doing it this way is that you get the standard “change” events fired from the model because you are setting the value of the attribute on the model.

    another way to do it would be to shortcut the process by using a get, and updating the array directly:

    myModel.get("myArray")[0] = 5
    myModel.trigger("change");
    myModel.trigger("change:myArray");
    
    myModel.get("myArray"); //=> [5, 1, 2]
    

    the disadvantage here is that this won’t fire the “change” events because you’re not calling the set method. so, if you need those events, you have to fire them yourself, as i’ve shown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this: var SomeNamespace = { Model: Backbone.Model.extend(), View: Backbone.View.extend({ model: new this.Model,
I have a Backbone.js Collection and I have an array of model IDs that
I have created a model in backbone.js. var Content = Backbone.Model.extend({ // If you
let's say I have : var Book = Backbone.Model.extend(); var Collection = Backbone.Collection.extend({ model:
I have the following model: var Soq = Backbone.model.extend({ default:{ 'name': 'something' , 'parents':
I have the following Parent Object: Context = { ContextModel: Backbone.Model.extend({ //model Code }),
I have a simple question about Backbone.js' get and set functions. 1) With the
I have a simple demo app like this Friend = Backbone.Model.extend name:null Friends =
Hey! I'm trying out Backbone.js and have followed the annotated source of the TODO's
I have a Backbone.Model named Company . My Company model has an Employees Backbone.Collection

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.