I am trying to write a function that returns all the item in the variant color array in the product array of objects; into the the "demo" ID. However the only thing that gets returned when I use document.getElementById() is the color "black".
product = [{
variants: [{
adjusted_price: "86000.00",
color: "red",
id: 421,
quantity: 20,
}]
},
{
brandName: "Femithz",
prodKeywords: "null",
variants: [{
adjusted_price: "54000.00",
color: "red",
converted_price: "145.60",
created_at: "2020-05-15T10:16:22.000000Z",
id: 421,
material: "Black",
pivot: {
product_id: 81,
variant_id: 421
},
price: "50000.00",
quantity: 20,
size: "women-s",
updated_at: "2020-05-15T10:16:22.000000Z",
variant_sku: "SKU-5ebe71dbc7b4f"
},
{
adjusted_price: "54000.00",
color: "black",
converted_price: "145.60",
created_at: "2020-05-15T10:16:22.000000Z",
id: 421,
material: "Black",
pivot: {
product_id: 81,
variant_id: 421
},
price: "40000.00",
quantity: 20,
size: "women-s",
updated_at: "2020-05-15T10:16:22.000000Z",
variant_sku: "SKU-5ebe71dbc7b4f"
}
]
}
]
var variant = 0;
for (var i = 0; i < product.length; i++) {
var variant = product[i].variants[i].color
var variantPrice = product[i].variants[i].price
var emptyArr = [];
emptyArr.push(variant)
// console.log(emptyArr)
// document.write(emptyArr)
// function checkColor(color) {
// return color === "green";
// }
document.getElementById("demo").innerHTML = emptyArr;
console.log(emptyArr)
}
<p id="demo"></p>