Add data in a single Json

Asked

Viewed 147 times

2

Hello

I’m having a problem and I’m having a hard time solving it.. Let’s go there

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

I need a single JSON with the 4 stock values in it, in order to be able to implement in my system and use it to remove my reports that I need so much

Example

I have it

{
    "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 it

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

Someone to give a light?

1 answer

1


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

An example would be 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);

Browser other questions tagged

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