简体   繁体   English

javascript从数组追加到变量

[英]javascript appending to a variable from an array

i am not sure if its a total n00b question but here goes. 我不确定是否总n00b问题,但这里去。

i have a JSON array with values: 我有一个带有值的JSON数组:

array[0].1 = "the value I want"

now I want to include all "the value I want" into this one variable like: 现在,我想将所有“我想要的值”包括到这个变量中,例如:

the value I want [1], the value I want [2]....

how do i do it? 我该怎么做? infact if there is a way I can get the comma seperated values into a variable as it is please let me know. 实际上,如果有一种方法可以将逗号分隔的值转换为变量,请告诉我。

EDIT: CLARIFICATION OF QUESTION 编辑:问题的澄清

I want to create a variable in which i want to append all the data from the JSON array i have. 我想创建一个变量,我要在其中添加我拥有的JSON数组中的所有数据。 for example, if the JSON data reads: 例如,如果JSON数据读取:

data[0] = value, data[1] = value, data[2] = value, ... 数据[0] =值,数据[1] =值,数据[2] =值,...

i want all the "value" appended into a variable. 我希望将所有“值”附加到变量中。

var mystring=array.join(", "); maybe? 也许?

var b = 'I,am,a,JavaScript,hacker'
var temp = new Array();
temp = b.split(',');

Now the string has been split into 5 strings that are placed in the array temp. 现在,该字符串已拆分为5个放在temp数组中的字符串。 The commas themselves are gone. 逗号本身不见了。

temp[0] = 'I';
temp[1] = 'am';
temp[2] = 'a';
temp[3] = 'JavaScript';
temp[4] = 'hacker.';

Taken from QuirksMode . 取自QuirksMode

join is the "opposite" of split - use it to append the elements of an array together into a variable. join是split的“对立面”-使用它可以将数组的元素一起附加到变量中。

var j = temp.join(',');  // sets j to 'I,am,a,JavaScript,hacker'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM