I'm trying to pass objects into an array within a for loop. The value keeps getting overwritten.
const files = require("./getFiles")();
module.exports = function templating() {
let design = JSON.parse(files[1]),
data = JSON.parse(files[2]),
count = Object.keys(data).length,
x = [];
for (let i = 0; i < count; i++) {
let tempDesign = design,
tempData = data;
tempDesign.columns[0].items[0]["url"] = tempData[i].url;
tempDesign.columns[1].items[0]["text"] = tempData[i].name;
tempDesign.columns[1].items[1]["text"] = tempData[i].desc;
tempDesign.columns[1].items[2]["facts"][0]["value"] = tempData[i].priceText;
tempDesign.columns[1].items[3]["actions"][0]["data"] = tempData[i].price;
x.push(tempDesign);
}
return x;
};
I want my output to look something like this :
sample= [
{
"x":"data",
"y":"data"
},
{
"a":"data",
"b":"data"
},
{
"c":"data",
"d":"data"
},
{
"e":"data",
"f":"data"
},
]
Instead I get this :
sample= [
{
"e":"data",
"f":"data"
},
{
"e":"data",
"f":"data"
},
{
"e":"data",
"f":"data"
},
{
"e":"data",
"f":"data"
}
]
The loop pushes only the last object to the array but with the right count.