简体   繁体   中英

Javascript push

I'm trying to push multiple values into an array.

When I use:

csvData.push('data[0][index],data[1][index],data[2][index],data[3][index]');

it formats it corrects so that

csvData[0] = "data[0][index],data[1][index],data[2][index],data[3][index]"
csvData[1] = "data[0][index],data[1][index],data[2][index],data[3][index]"
etc

Also when it evolves into a csv, it is correct in the 4 columns.

However when I use no quotes:

csvData.push(data[0][index],data[1][index],data[2][index],data[3][index]); 

I get the values I want, but it's single dimensional.

csvData[0] = 23
92
74
22
etc

instead of

csvData[0] = 23,92,74,22

How can I add values it correctly?

You can create an array and push that:

csvData.push( [ data[0][index],data[1][index],data[2][index],data[3][index] ] ); 

The added brackets ( [ ] ) around the list of values creates an array, and that in turn is what's pushed onto the csvData array.

将它们放入数组中,并使用join()方法将其join()然后将其推送。

csvData.push([data[0][index],data[1][index],data[2][index],data[3][index]].join()); 

如果您想要多维而不是使用

csvData.push([data[0][index],data[1][index],data[2][index],data[3][index]]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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