Preciso criar uma função que receba uma lista e um schema. Esta função deve transformar a lista em um objeto com o mesmo formato to schema passado.
Exemplo de lista:
const rows = [
{
"id": "f9d0d801-9645-4d09-b49b-b84b572fd918",
"name": "Produto 1",
"price": null,
"attributes.id": "23d687b6-565f-469f-9aec-84558cfec692",
"attributes.name": "tamanho",
"attributes.type": "select",
"attributes.multiple": false,
"attributes.price": 0,
"attributes.options.id": "345180c1-381d-495d-9645-2a075ca832e3",
"attributes.options.text": "P",
"attributes.options.price": 15
},
{
"id": "f9d0d801-9645-4d09-b49b-b84b572fd918",
"name": "Produto 1",
"price": null,
"attributes.id": "23d687b6-565f-469f-9aec-84558cfec692",
"attributes.name": "tamanho",
"attributes.type": "select",
"attributes.multiple": false,
"attributes.price": 0,
"attributes.options.id": "15562510-2bbe-4140-808a-7527c14d8044",
"attributes.options.text": "M",
"attributes.options.price": 25
},
{
"id": "f9d0d801-9645-4d09-b49b-b84b572fd918",
"name": "Produto 1",
"price": null,
"attributes.id": "23d687b6-565f-469f-9aec-84558cfec692",
"attributes.name": "tamanho",
"attributes.type": "select",
"attributes.multiple": false,
"attributes.price": 0,
"attributes.options.id": "9fda61e0-b4cc-49bc-a099-7abd0458615e",
"attributes.options.text": "G",
"attributes.options.price": 30
},
{
"id": "f9d0d801-9645-4d09-b49b-b84b572fd918",
"name": "Produto 1",
"price": null,
"attributes.id": "23d687b6-565f-469f-9aec-84558cfec692",
"attributes.name": "tamanho",
"attributes.type": "select",
"attributes.multiple": false,
"attributes.price": 0,
"attributes.options.id": "b0c688db-db9d-42f4-91fe-f9a4c66cba4b",
"attributes.options.text": "GG",
"attributes.options.price": 35
},
{
"id": "f9d0d801-9645-4d09-b49b-b84b572fd918",
"name": "Produto 1",
"price": null,
"attributes.id": "464d23d6-55a2-4fca-a591-eeb8bbee5405",
"attributes.name": "embrulhar",
"attributes.type": "boolean",
"attributes.multiple": false,
"attributes.price": 2,
"attributes.options.id": null,
"attributes.options.text": null,
"attributes.options.price": null
}
]
Exemplo de schema:
export const ProductSchema = {
id: 'string',
name: 'string',
price: 'number',
attributes: [
{
id: 'string',
name: 'string',
type: 'string',
multiple: 'boolean',
price: 'number',
options: [
{
id: 'string',
text: 'string',
price: 'number'
}
]
}
]
}
O resultado esperado para esta lista e este schema é:
{
"id": "f9d0d801-9645-4d09-b49b-b84b572fd918",
"name": "Produto 1",
"price": null,
"attributes": [
{
"id": "23d687b6-565f-469f-9aec-84558cfec692",
"name": "tamanho",
"type": "select",
"multiple": false,
"price": 0,
"options": [
{
"id": "345180c1-381d-495d-9645-2a075ca832e3",
"text": "P",
"price": 15
},
{
"id": "15562510-2bbe-4140-808a-7527c14d8044",
"text": "M",
"price": 25
},
{
"id": "9fda61e0-b4cc-49bc-a099-7abd0458615e",
"text": "G",
"price": 30
},
{
"id": "b0c688db-db9d-42f4-91fe-f9a4c66cba4b",
"text": "GG",
"price": 35
}
]
},
{
"id": "464d23d6-55a2-4fca-a591-eeb8bbee5405",
"name": "embrulhar",
"type": "boolean",
"multiple": false,
"price": 2,
"options": []
}
]
}
Não encontrei nenhuma biblioteca no NPM que faça esse trabalho.