WraithLux
WraithLux

Reputation: 699

javascript: multidimensional array manipulation

I have something like this (data should be a global variable):

var data = {
    fields:{
      id: 0,
      ticket: 0,
      description: 0,
    }
}

Now I want to use something like this to change these variables:

function toggleStatus(element) {
data[fields][element] = 1;
}

This doesn't work, of course, but what is the correct way to manipulate data in similar fashion?

Basically, I need to create a multidimensional array that changes it's status based on user input.

Upvotes: 0

Views: 592

Answers (3)

nebulae
nebulae

Reputation: 2665

just a note, if you're dealing with arrays of objects, it would look more like this:

  var data = [{
        fields:[{
          id: 0,
          ticket: 0,
          description: "bar"
         },
         {
          id: 1,
          ticket: 1,
          description: "foo"
         }] 
  }];

then you could access the properties like

data[0].fields[0].id

data[0].fields[1].description = "more foo"

or

data[0].fields[1]['description'] = "more foo"

Upvotes: 0

Erik Christianson
Erik Christianson

Reputation: 507

if element is passed in as one of the names of the properties of field, this should work.

Try:

data['fields']['id'] = 1;

Maybe this would work?

Upvotes: 0

tymeJV
tymeJV

Reputation: 104795

That should work fine, but you have to enclose fields in quotes:

data['fields'][element] = 1;

Or

data.fields[element] = 1;

Upvotes: 2

Related Questions