Merging Values into an Array

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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

回答1:

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" 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!