1

How to best create this in runtime?

var fields = ["abc", "def", "somepar", "otherpar"];
var toSet = "foo";
// How to genereate this variable from code
var valuepairs = [["Progress", progress], [fields[0], toSet], [fields[1], toSet], [fields[2], toSet]]; // [fields[n], toSet]

Regards

Larsi

2
  • What are you asking here? The code you gave here is completely valid. How do you need to use it? Commented Mar 14, 2011 at 10:29
  • @picardo: Seems pretty clear. He gives the inputs and asks how to programmatically generate the given output. Commented Mar 14, 2011 at 12:21

2 Answers 2

1

Use the .push method to append to an array...

var valuepairs = [["Progress", progress]];
for (var k = 0; k < fields.length; ++ k)
   valuepairs.push([fields[k], toSet]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that was just the function I was looking for
@Larsi: fyi valuepairs[valuepairs.length] = [fields[k], toSet]; works too, not that I'm particularly advocating it over this solution.
0
var valuepairs = new Array(new Array('progress',progress)); //first line is hardcoded...
for (var i=0; i<fields.length; i++) {
   valuepairs.push(new Array(fields[i],toSet));
}

2 Comments

Sorry I was a bit late accepting, but Kenny was first, so I'll accept his answer. Thanks for your time!
Thanks Larsi. I am not here for the reps, just for fun. Good luck.

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.