javascript 将javascript变量存储到数组中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18546038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:21:11  来源:igfitidea点击:

Store javascript variable into array

javascriptarraysvariables

提问by user2735129

Is it possible to store variables into arrays? What is the correct way of doing it? I can't seem to be able to store it in this way.

是否可以将变量存储到数组中?正确的做法是什么?我似乎无法以这种方式存储它。

var fxVal = '<?php echo $fxVal;?>';
var equitiesVal = '<?php echo $equitiesVal;?>';
var boVal = '<?php echo $boVal;?>';
var balance = '<?php echo $account_balance;?>';

var myData = [fxVal,equitiesVal,boVal,balance];

回答by cl0udw4lk3r

There are several ways to do it:

有几种方法可以做到:

var array1 = [var1, var2, var3, var4];
var array2 = new Array(var1, var2, var3, var4);

or

或者

var array3 = new Array;

array3.push(var1);
array3.push(var2);
array3.push(var3);
array3.push(var4);

回答by elclanrs

Take a look into JSON as data format.

看看作为数据格式的 JSON。

var data = <?= json_encode([$fxVal, $equitiesVal, $boVal, $account_balance]); ?>;

When you want to send or receive more data consider using AJAX.

当您想要发送或接收更多数据时,请考虑使用 AJAX。