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
Post a Comment