简体   繁体   English

格式化javascript数组

[英]Format javascript array

I have following variables: 我有以下变量:

a = {y: 3, color: colors[0]}; 
b = {y: 5, color: colors[0]}; 
c = {y: 5, color: colors[0]}; 
d = {y: 3, color: colors[0]}; 

And I want something like this: 我想要这样的东西:

r = {y: 3, color: colors[0]}, 
    {y: 5, color: colors[0]}, 
    {y: 5, color: colors[0]}, 
    {y: 3, color: colors[0]}; 

How I can do this with these four variables? 如何使用这四个变量执行此操作? I tried something like: 我尝试了类似的东西:

r = a+b+c+d; 

But this doesn't work. 但这是行不通的。

Thanks 谢谢

If you want an array of objects, then first you construct an array ( [...] ) then objects inside the array ( {...} ). 如果需要对象数组,则首先构造一个数组( [...] ),然后构造该数组内的对象( {...} )。 Thus you end up with: 因此,您最终得到:

var r = [
    {y: 3, color: colors[0]}, 
    {y: 5, color: colors[0]},
    {y: 5, color: colors[0]},
    {y: 3, color: colors[0]}
];

Or, with your existing variables var r = [a, b, c, d]; 或者,使用您现有的变量var r = [a, b, c, d];

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

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