简体   繁体   中英

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];

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