Store variable into array javascript - Stack Overflow

How to store the javascript variable into array?I have these variable and I wish to store them into arr

How to store the javascript variable into array?

I have these variable and I wish to store them into array:

var name=document.forms["form"]["name"].value;
    var email=document.forms["form"]["email"].value;
    var mobile=document.forms["form"]["mobile"].value;
    var q1=document.forms["form"]["q1"].value;
    var q2=document.forms["form"]["q2"].value;
    var q3=document.forms["form"]["q3"].value;
    var l1=document.forms["form"]["logo1"].value;
    var l2=document.forms["form"]["logo2"].value;
    var l3=document.forms["form"]["logo3"].value;
    var p1=document.forms["form"]["photo1"].value;
    var p2=document.forms["form"]["photo2"].value;
    var p3=document.forms["form"]["photo3"].value;

How to store the javascript variable into array?

I have these variable and I wish to store them into array:

var name=document.forms["form"]["name"].value;
    var email=document.forms["form"]["email"].value;
    var mobile=document.forms["form"]["mobile"].value;
    var q1=document.forms["form"]["q1"].value;
    var q2=document.forms["form"]["q2"].value;
    var q3=document.forms["form"]["q3"].value;
    var l1=document.forms["form"]["logo1"].value;
    var l2=document.forms["form"]["logo2"].value;
    var l3=document.forms["form"]["logo3"].value;
    var p1=document.forms["form"]["photo1"].value;
    var p2=document.forms["form"]["photo2"].value;
    var p3=document.forms["form"]["photo3"].value;
Share Improve this question asked Oct 11, 2013 at 7:30 youaremysunshineyouaremysunshine 3517 gold badges17 silver badges28 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1
var arr = [];
var name=document.forms["form"]["name"].value;
var email=document.forms["form"]["email"].value;
arr.push(name);
//etc

Using the .push() method

You could also serialize if you are going to post the form.

As simple as

var newArray = [];
newArray[0] = name;
newArray[1] = email;

...

You can try with traditional array:

var myArray = [];
myArray.push(document.forms["form"]["name"].value);

The keys will be numeric (starting from 0).
Or, if you want to preserve string keys, like associative arrays in other languages, you can store your values as an object

var myArray = {};
myArray["name"] = document.forms["form"]["name"].value;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744966284a4603698.html

相关推荐

  • Store variable into array javascript - Stack Overflow

    How to store the javascript variable into array?I have these variable and I wish to store them into arr

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信