6 Replies - 1798 Views - Last Post: 26 June 2013 - 05:06 PM

#1 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Dynamic keys in associative arrays

Posted 25 June 2013 - 08:22 PM

Hi all,

I'm creating associative arrays for use with the JQuery Validation plugin, like this:

rules: {
    name: {
        required: true
    }
}



And when I can hard-code the rule types, everything works fine. But, if I try to do a dynamic rule type with a variable in the associative array key, like this:

// This variable value may be one of many different values in the actual code.
var specific = "test";
rules: {
    name: {
        'rule' + specific: true
    }
}



I get a syntax error.

So, my question is, how can I dynamically create the associative array key (which is used as the JQuery method name) correctly? I can't seem to figure it out, and I'd appreciate any help.

Thanks,
Zel2008

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic keys in associative arrays

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,169
  • Joined: 08-June 10

Re: Dynamic keys in associative arrays

Posted 25 June 2013 - 09:04 PM

View PostZel2008, on 26 June 2013 - 05:22 AM, said:

So, my question is, how can I dynamically create the associative array key correctly?

1) its not an associative array, but an object (it lacks array methods and is not an instance of Array)
2) not via literal syntax, you need to use the bracket syntax:
rules.name['rule'+specific] = true;

Was This Post Helpful? 1
  • +
  • -

#3 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Dynamic keys in associative arrays

Posted 25 June 2013 - 09:06 PM

Well, I guess I completely misunderstood what I was looking at. Thanks for the explanation, it makes a lot more sense now.

EDIT: Sorry, I just realized I have one more syntax problem. What would the syntax be to set something dynamic one object deeper, like:

var specific = "test";
rules: {
    name: {
        'rule' + specific: true,
        data: { 'rule' + specific: moreData }
    }
}



I tried:

rules.name[data]['rule'+specific] = moreData



But no dice, I get a syntax error again.

This post has been edited by Zel2008: 25 June 2013 - 10:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 436
  • View blog
  • Posts: 1,847
  • Joined: 30-April 10

Re: Dynamic keys in associative arrays

Posted 26 June 2013 - 08:35 AM

Where does it say the error for the syntax is?

if this is the validator that you are using it is using a simple object not an array.

This post has been edited by laytonsdad: 26 June 2013 - 08:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 436
  • View blog
  • Posts: 1,847
  • Joined: 30-April 10

Re: Dynamic keys in associative arrays

Posted 26 June 2013 - 08:48 AM

In an object you cant have the + in the key.

This post has been edited by laytonsdad: 26 June 2013 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,169
  • Joined: 08-June 10

Re: Dynamic keys in associative arrays

Posted 26 June 2013 - 12:13 PM

View PostZel2008, on 26 June 2013 - 06:06 AM, said:

EDIT: Sorry, I just realized I have one more syntax problem. What would the syntax be to set something dynamic one object deeper, like:

same answer as before:

View PostDormilich, on 26 June 2013 - 06:04 AM, said:

2) not via literal syntax, you need to use the bracket syntax:

Was This Post Helpful? 0
  • +
  • -

#7 Zel2008  Icon User is offline

  • D.I.C Addict

Reputation: 14
  • View blog
  • Posts: 790
  • Joined: 06-January 09

Re: Dynamic keys in associative arrays

Posted 26 June 2013 - 05:06 PM

Thanks again all, I got it -- using the wrong syntax again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1