Add data in a single Json

2

Hello

I have a problem and I'm having a hard time solving it .. come on

I have 4 JSON files exactly the same with 22,000 product records, same parameters and everything, the only difference between them is that each has a different stock value (there are 4 stores each with your stock)

I need a unique JSON with the 4 stock values in it , so I can deploy to my system and use it to pull out my reports that I both need

Example

I have this

{
    "id": 1,
    "nome": produto1,
    "valor": 20.0,
    "estoque": 5
},
{
    "id": 1,
    "nome": produto1,
    "valor": 20.0,
    "estoque": 2
},
{
    "id": 1,
    "nome": produto1,
    "valor": 20.0,
    "estoque": 3
}

And I need this

{
    "id": 1,
    "nome": produto1,
    "valor": 20.0,
    "estoque": 5,
    "estoque2": 2,
    "estoque3": 3
}

Someone to give a Light?

    
asked by anonymous 25.11.2017 / 05:31

1 answer

1

If everyone has the same files, the thing is simple, you just have to create a loop and use the loop index to extract the values.

An example would look like this:

const lojaA = [{
  "id": 1,
  "nome": 'produto1',
  "valor": 20.0,
  "estoque": 5
}, {
  "id": 2,
  "nome": 'produto2',
  "valor": 23.0,
  "estoque": 1
}];

const lojaB = [{
  "id": 1,
  "nome": 'produto1',
  "valor": 20.0,
  "estoque": 3
}, {
  "id": 2,
  "nome": 'produto2',
  "valor": 23.0,
  "estoque": 7
}];

const lojaC = [{
  "id": 1,
  "nome": 'produto1',
  "valor": 20.0,
  "estoque": 1
}, {
  "id": 2,
  "nome": 'produto2',
  "valor": 23.0,
  "estoque": 10
}];

const todos = lojaA.map((produto, index) => {
  return Object.assign(produto, {
    estoque2: lojaB[index].estoque,
    estoque3: lojaC[index].estoque
  }, {});
});

console.log(todos);
    
25.11.2017 / 08:55