I have object that has multiple values for example
let obj = {
a: "day1",
b: "",
c: "day3",
aa: 10,
bb: 11,
cc: 12,
}
let data = {};
let item = [];
for (let i in obj) {
if (i === 'a') {
data["title"] = obj.a;
data['value'] = obj.aa;
}
if (i === 'b') {
data["title"] = obj.b;
data['value'] = obj.bb;
}
if (i === 'c') {
data["title"] = obj.c;
data['value'] = obj.cc;
}
item.push(data);
}
console.log(item)
but I'm getting only last day3 value in multiple times .
item [
{title:"day3",value:12},
{title:"day3",value:12},
{title:"day3",value:11}
]
I want in the following format
item [
{title:"day1",value:10},
{title:"day3",value:11}
]
please help, thanks in advance.
i === 'day2'butiwill be equal to the current property name not value. You should really break down your problems and verify its parts before addressing the problem as a wholetitleandvalue) at one and the samedataobject. The OP wants to use an array and push at/each time a newly createddataobject into it.