'Getting circular object in map function
I have a big array of objects, in which I have to use two particular object keys for each element custom_attributes and custom_options. For each attribute one flavor from custom_options will be added and price will be noted. So if we have 3 attributes and 7 flavours we will get an array of length 7 with every attributes and 1 flavor in each element with different prices.
"custom_attributes": [
{
"attribute_code": "shape",
"value": "Round"
},
{
"attribute_code": "weight",
"value": "1"
},
{
"attribute_code": "product_type",
"value": "NEG"
}
],
"custom_options": [
{
"attribute_code": "flavor",
"values": [
{
"value": "1",
"additional_price": 0,
"slug": "vanilla",
"label": "Vanilla",
"image": null
},
{
"value": "24",
"additional_price": 100,
"slug": "pineapple-vanilla",
"label": "Pineapple Vanilla",
"image": null
},
{
"value": "2",
"additional_price": 100,
"slug": "butterscotch",
"label": "Butterscotch",
"image": null
},
{
"value": "10",
"additional_price": 100,
"slug": "black-forest",
"label": "Black Forest",
"image": null
},
{
"value": "20",
"additional_price": 200,
"slug": "chocolate-truffle",
"label": "Chocolate Truffle",
"image": null
},
{
"value": "5",
"additional_price": 150,
"slug": "blueberry",
"label": "Blueberry",
"image": null
},
{
"value": "4",
"additional_price": 150,
"slug": "strawberry",
"label": "Strawberry",
"image": null
},
{
"value": "13",
"additional_price": 200,
"slug": "red-velvet",
"label": "Red Velvet",
"image": null
},
{
"value": "23",
"additional_price": 150,
"slug": "white-forest",
"label": "White Forest",
"image": null
},
{
"value": "6",
"additional_price": 200,
"slug": "mixed-fruit",
"label": "Mixed Fruit",
"image": null
},
{
"value": "9",
"additional_price": 150,
"slug": "mango",
"label": "Mango",
"image": null
},
{
"value": "1615798387075",
"additional_price": 200,
"slug": "irish_coffee",
"label": "Irish Coffee",
"image": null
},
{
"value": "1615798423428",
"additional_price": 300,
"slug": "death_by_chocolate",
"label": "Death By Chocolate",
"image": null
},
{
"value": "1615798462183",
"additional_price": 250,
"slug": "vancho",
"label": "Vancho",
"image": null
},
{
"value": "1615287938626",
"additional_price": 230,
"slug": "gulab_jamun_vanilla",
"label": "Gulab Jamun Vanilla",
"image": null
},
{
"value": "1615287902661",
"additional_price": 250,
"slug": "gulab_jamun_red_velvet",
"label": "Gulab Jamun Red Velvet",
"image": null
},
{
"value": "26",
"additional_price": 350,
"slug": "rasmalai",
"label": "Rasmalai",
"image": null
}
],
"min_select": 1,
"max_select": 1
}
],
The code below is what I'm using now but the end result I get is a circular object.
const variants = configurable_children.map(variant => {
const vop = variant.custom_attributes.map(k => {
if(k.attribute_code === "weight")
{
const weight_value = (attributes[attributes.findIndex(el => el.slug === k.attribute_code)].options[k.value].slug);
return ({
'name': k.attribute_code,
"value": weight_value
})
}
else if(k.attribute_code === "shape"){
const shape_value = (attributes[attributes.findIndex(el => el.slug === k.attribute_code)].options[k.value].slug);
return ({
'name': k.attribute_code,
"value": shape_value
})
}
else if(k.attribute_code === "product_type"){
return ({
'name': k.attribute_code,
"value": k.value === "EG" ? "egg" : "eggless"
})
}
});
if(variant.custom_options.length === 0){
return ({
'price': variant.discount_price,
'compare_price': variant.final_price,
'sku': variant.sku,
"options": vop
})
}else{
//map over all custom_fields as well
let options = variant.custom_options[0].values.map(op => {
return ({
'price': variant.discount_price + op.additional_price,
'compare_price': variant.final_price +op.additional_price,
'sku': variant.sku,
"options": [ ...vop,{name: variant.custom_options[0].attribute_code, value: op.slug }]
})
});
console.log("options flavour: -------------");
console.log(options);
return options
}
});
The issue is happening in the last return statement of last else statement where I'm spreading the the vop array I got before to all the new mapped values and returning. I'm getting circular object error.
Should I change my approach or Am I missing something here ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
