0
I’m having trouble turning data into json.
I have the following code below that takes the elements of my payload and print to json.
const _ = require('lodash')
let prod = [];
let stores = [];
module.exports = async (data) => {
data.map(Element => {
let dado = {}
dado.code = Element.COD_PROD,
dado.stores = stores
prod.push(Object.assign({}, dado))
})
data.map(Element => {
let dado = {};
let a = [];
let V_MESES;
V_MESES = Element.MESES
a = V_MESES.split(",");
let pos = _.findIndex(stores, entr => {
return entr.code === Element.COD_LOJA
})
if (pos === -1) {
for (let i = 0; i < 12; i++) {
dado.code_prod = Element.COD_PROD,
dado.code = Element.COD_LOJA,
dado.value = `${a[i]}`,
stores.push(Object.assign({}, dado));
}
}
})
return prod
}
payload:
[
[
{
"COD_LOJA": 10,
"ANO": 2016,
"COD_PROD": 1,
"MESES": "1.00,0.00,2.00,0.00,0.00,1.00,3.00,0.00,0.00,1.00,0.00,0.00"
},
{
"COD_LOJA": 10,
"ANO": 2016,
"COD_PROD": 2,
"MESES": "2.00,3.00,4.00,0.00,0.00,1.00,3.00,0.00,0.00,1.00,0.00,0.00"
}
] ]
No Output :
[
{
"code": 1,
"stores": [
{
"code_prod": 1,
"code": 10,
"value": "1.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "2.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "1.00"
},
{
"code_prod": 1,
"code": 10,
"value": "3.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "1.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
}
]
},
{
"code": 2,
"stores": [
{
"code_prod": 1,
"code": 10,
"value": "1.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "2.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "1.00"
},
{
"code_prod": 1,
"code": 10,
"value": "3.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "1.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
},
{
"code_prod": 1,
"code": 10,
"value": "0.00"
}
]
}
]
My doubt is how do I get product 2 to pick up the data belonging to it? Because product 2 prints the product data one.