Doubt in Javascript Map and findIndex

Asked

Viewed 47 times

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.

1 answer

0

You have declared an array stores and assigned that same array to both scores of output.

Since arrays are passed by reference, both arrays are the same, so when you add an item to an array, it reflects on the other.

There are more errors in the code, there is no reason for this function to be async, you use map instead of forEach to iterate on arrays, this findIndex It makes no sense in the context of your example. If it’s to follow your logic, I believe that after you tidy up, your code would look something like this:

function refatorar(data) {
  let prod = []; // prod é declarado no escopo da função, não do módulo

  for (let elem of data) {
    let dado = {};
    dado.code = elem.COD_PROD;
    dado.stores = []; // um "stores" novo para cada dado

    let v_meses = elem.MESES.split(',');

    for (let mes of v_meses) {
      let store = {};
      store.code_prod = elem.COD_PROD;
      store.code = elem.COD_LOJA;
      store.value = mes;

      dado.stores.push(store);
    }

    prod.push(dado);
  }

  return prod;
}


const data = [{
  "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"
}]

console.log(refatorar(data));

Of course, you can also make proper use of the method map to generate new arrays:

function refatorar(data) {
  return data.map(elem => ({
    code: elem.COD_PROD,
    stores: elem.MESES.split(',').map(mes => ({
      code_prod: elem.COD_PROD,
      code: elem.COD_LOJA,
      value: mes
    }))
  }))
}

const data = [{
  "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"
}];

console.log(refatorar(data));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.