Cart with dynamic options with Angularjs

0

I have the following problem: how to subtract 2 arrays with angularjs.

I have tried in several ways and I have not been able to do this subtraction.

I'm creating a product cart with optional dynamic angularjs

In this case the client made the following modification:

increment the id 1004, 1000 decrement the id 1001 added the id 1002

Original recipe:

[{"id":1004,"idproduto":3,"forma":"Alface","preco":1,"quantidade":1},{"id":1000,"idproduto":3,"forma":"Bacon","preco":2,"quantidade":1},{"id":1001,"idproduto":3,"forma":"Queijo Cheedar","preco":2,"quantidade":3}]

modified recipe:

[{"id":1004,"idproduto":3,"forma":"Alface","preco":1,"quantidade":2},{"id":1000,"idproduto":3,"forma":"Bacon","preco":2,"quantidade":2},{"id":1001,"idproduto":3,"forma":"Queijo Cheedar","preco":2,"quantidade":1},{"id":1002,"idproduto":2,"forma":"Bacon","preco":2,"quantidade":1}]

I need to reformat this array because I need to show the changes in the shopping cart to the client.

Product 500

+1 1004 (R $ 1.00)

+1 1000 ($ 2.00)

+1 1002

-2,100 (R $ 4.00)

Final recipe for the cart

[{"id":1004,"idproduto":3,"forma":"Alface","preco":1,"quantidade":1},{"id":1000,"idproduto":3,"forma":"Bacon","preco":2,"quantidade":1},{"id":1001,"idproduto":3,"forma":"Queijo Cheedar","preco":2,"quantidade":-2},{"id":1002,"idproduto":2,"forma":"Bacon","preco":2,"quantidade":1}]

And in the end you would have to add the original array with the end to put the stock down.

Am I correct in this logic?

    
asked by anonymous 03.03.2015 / 00:33

1 answer

1

The name of this operation is diff, and you want to get the delta (difference) between the two collections.

There is a library called jsondiffpatch (link) that may be useful to you. The output is the delta (difference) between two sources.

The above example results in the following delta according to this library :

{
  "2": {
    "quantidade": [
      3,
      -2
    ]
  },
  "3": [
    {
      "id": 1002,
      "idproduto": 2,
      "forma": "Bacon",
      "preco": 2,
      "quantidade": 1
    }
  ],
  "_t": "a"
}
    
03.03.2015 / 01:41