javascript - How to push data twice into object -


is there possible push data object twice , 1 result? have code:

var t = data.datasets,  var data = {     datasets : (function(){         var arr = [];         (var i=0; < t.length; i++) {             arr.push({                 label: t[i].label,                 data: t[i].values             });             if (t[i].color === "red"){                 arr.push({                     fillcolor: "rgba(223, 34, 155, 0.48)"                 });             } else {                 arr.push({                     fillcolor: "rgba(167, 167, 167, 0.64)"                 });             }         }         return arr;     })() }; 

on , need this:

arr.push({     label: t[i].label,     data: t[i].values,     fillcolor: "rgba(167, 167, 167, 0.64)" // or other }); 

or must push repetitive values include on each condition?

why not this:

    (var i=0; < t.length; i++) {         var temp = {             label: t[i].label,             data: t[i].values         };         if (t[i].color === "red"){             temp.fillcolor = "rgba(223, 34, 155, 0.48)";         } else {             temp.fillcolor: = "rgba(167, 167, 167, 0.64)";         }         arr.push(temp);     } 

that way don't have keep repeating label , data part.

or, if have 2 possible values fillcolor (you more, it'll mess), this:

arr.push({       label: t[i].label,       data: t[i].values,       fillcolor: t[i].color === "red" ? "rgba(223, 34, 155, 0.48)" : "rgba(167, 167, 167, 0.64)"   }); 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -