-1

I've got this function that defines a random color set.

My issue is defining the variable (var target) to select the new color set from the kuler object.

kuler: {
    set0:['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9'],
    set1:['#0F2440','#2C3F59','#518C8C','#94BEAC','#D9CAAD'],
    set2:['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9'],
    set3:['#0F2440','#2C3F59','#518C8C','#94BEAC','#D9CAAD'],
    set4:['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9'],
    set5:['#0F2440','#2C3F59','#518C8C','#94BEAC','#D9CAAD'],
    set6:['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9']
},
setNewColourSet: function () {
    rn=Math.floor(Math.random()*5);
    for (i=0; i<4; i++){
        var target = "project.kuler.set"+rn+"["+i+"]";
        $('.kuler'+i).css('background-color',target);
        /* works *///  $('.kuler'+i).css('background-color',project.kuler.set4[i]);
    }
}
3
  • kuler is a property of a much larger object. Commented Aug 21, 2012 at 13:21
  • correct, i left out the rest of the object. Commented Aug 21, 2012 at 13:23
  • This question is similar to: Accessing an object property with a dynamically-computed name. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 8, 2024 at 22:06

4 Answers 4

3

In think you were close -- just put the string within the square braces.

var target = project.kuler['set' + rn][i];

assuming rn is the number appended to 'set', and kuler is itself in an object literal called project.

Note the first brace (kuler['set'+rn]) is property access on the object literal kuler, and the second brace ([i])is array index access on an array....

Sign up to request clarification or add additional context in comments.

1 Comment

yeah, i need to wait 2mins before it will let me answer. :(
1
kuler: {
    set0:['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9'],
    set1:['#0F2440','#2C3F59','#518C8C','#94BEAC','#D9CAAD'],
    set2:['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9'],
    set3:['#0F2440','#2C3F59','#518C8C','#94BEAC','#D9CAAD'],
    set4:['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9'],
    set5:['#0F2440','#2C3F59','#518C8C','#94BEAC','#D9CAAD'],
    set6:['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9']
},
setNewColourSet: function () {
    var rn = Math.floor(Math.random()*5);

    for (i=0; i<4; i++){
    $('.kuler'+i).css('background-color', this.kuler['set'+i][rn]);
    }
}

here you go

Comments

0

If you're going to have an object with numbered names like this, why not just have a list?

kuler: [
  ['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9'],
  ['#0F2440','#2C3F59','#518C8C','#94BEAC','#D9CAAD'],
  ['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9'],
  ['#0F2440','#2C3F59','#518C8C','#94BEAC','#D9CAAD'],
  ['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9'],
  ['#0F2440','#2C3F59','#518C8C','#94BEAC','#D9CAAD'],
  ['#499E8D','#85CC9F','#A4DEAB','#C8E8C7','#FFF6C9']
],

setNewColourSet: function () {
  rn=Math.floor(Math.random()*5);
  for (i=0; i<4; i++){        
    $('.kuler'+i).css('background-color',kuler[rn][i]);
  }
}

2 Comments

thank you, this makes sense. unsure why i didn't do this in the first place.
Sometimes we over-engineer our solutions :)
0

var target = "project.kuler.set"+rn+"["+i+"]";

You are assigning a string to a target.

I suppose project is an object and the kuler is a part of it so try with removing quotes;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.