0

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.

3 Answers 3

2

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

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

Or

data.fields[element] = 1;
Sign up to request clarification or add additional context in comments.

3 Comments

In addition, you can do: data.fields.element = 1;
@Shadowedged -- No you can't. .element will be interpreted as looking for a property called element. FYI
Ah, you are right. I misunderstood the question. I was under the impression @WraithLux wanted to add an element attribute to his field object.
0

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?

Comments

0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.