0๐
โ
If I got the idea correctly:
const status = {
"grid" : {
"accent" : [
[
{
"color" : "property"
},
{
"height" : "property"
}
]
]
},
"inline" : {
"accent" : [
[
{
"color" : "property"
},
{
"size" : "property"
},
{
"capacity" : "property"
}
]
]
}
}
const product = [
[
"size",
{
"name":"Size",
"code":"size",
"propertiesList":[
{
"value":"50",
"code":"50sm"
}
]
}
],
[
"capacity",
{
"name":"Capacity",
"code":"capacity",
"propertiesList":[
{
"value":"0.75",
"code":"750ml"
}
]
}
],
[
"height",
{
"name":"Height",
"code":"height",
"propertiesList":[
{
"value":"Low",
"code":"low"
}
]
}
],
[
"color",
{
"name":"Color",
"code":"color",
"propertiesList":[
{
"value":"Red",
"code":"red"
}
]
}
]
]
function getPropertyValue(product, key) {
const item = product.find(item => item[0] === key)
return item?.[1]?.propertiesList?.[0]?.value
}
function formatStatus(status, product, viewStatus) {
// creating a deep copy to avoid mutating the original object
const currentStatus = JSON.parse(JSON.stringify(status[viewStatus]))
currentStatus.accent[0].forEach(item => {
for (const key in item) {
item[key] = getPropertyValue(product, key)
}
})
return { [viewStatus]: currentStatus }
}
console.log(formatStatus(status, product, "grid"))
Source:stackexchange.com