0

I have a situation where i have to manually merge a label with value and then store in array. For instance aaa 10 , bbb 20, ccc 30

The values are coming from text field and finally i have to bring this in such format... with comma seperated and label's are hardcoded.

How to create an Array or a String like this aaa 10 , bbb 20, ccc 30 with Key:Value pair

theJava
  • 14,620
  • 45
  • 131
  • 172
  • 6
    uh, so what's the question? – Andy E Apr 06 '11 at 15:35
  • 2
    Where *exactly* do the values come from? What do 'aaa' and '10' represent? – gen_Eric Apr 06 '11 at 15:44
  • This is why it's important to write your question really well, so nobody wastes their time guessing at what you want. Your other question is much clearer but should be merged with this one. – Andy E Apr 06 '11 at 16:09

1 Answers1

4

I'm not exactly sure what you're asking for, but perhaps this helps

//create array
var list = [];

//get value from input aaa
var value1 = document.getElementById("aaa").value;
//add items
list.push("aaa "+value1);

//get value from input bbb
var value2 = document.getElementById("bbb").value;
//add items
list.push("bbb "+value2);

//get value from input ccc
var value2 = document.getElementById("ccc").value;
//add items
list.push("bbb "+value2);

//this gives you an array like this ["aaa 10", "bbb 20", "ccc 30"]

//to create a string from that you can simply call join
var result = list.join(); //result = "aaa 10, bbb 20, ccc 30"
SavoryBytes
  • 35,571
  • 4
  • 52
  • 61