Add items to an array without duplicating them

0

Friends good Sunday, I need to add data to an Array, I'm making a kind of shopping cart and I'm at the point where I need to add to a list the products that the end customer wants to buy, the way I use is as follows:

<div class="col-sm-4 text-right" style="width:90px">
 <h-number value="inicial" min="0" max="10" step="1" change="realizarSuma(co,inicial)"></h-number>
     <div  class="precio">
        ${{co.precio*inicial}}
     </div>
 </div>

This goes like this: Through a number-picker I assign value="inicial" that is $scope.inicial = 0; and it goes adding or subtracting, in the value change="realizarSuma(co,inicial)" that is like a kind of ng-change that reacts to the change of the buttons + o - I send co that corresponds to an array with all the caffe information available (in this case it sends 3 array) and finally command the value inicial which is the number of the amount that was requested.

$scope.realizarSuma = function(item,inicial){

  var producto = item.producto;
  var contador = inicial;
  var precio = item.precio; 
  var numerico = item.numerico; //identificador
  var preciototal = precio*contador; //total de compra por producto
  $scope.coffedate = [];

  $scope.coffedate.push({total:preciototal,identificador:numerico})

  console.log($scope.coffedate);
}

What I do here after passing the data to a variable is to add the data to an array to measures that are closed by pressing + or - but the problem is that the value is replaced (as seen in the image) and that It does not work for me.

I think I need to receive the data with a condition, for that I have numerico that is a unique value of each product, so I tried to do a kind of search with for when pressing + or - to scroll through the for and find if there is numerico and if it exists that saves the last value.

but something is wrong, please help!

    
asked by Hernan Humaña 22.01.2017 в 21:40
source

2 answers

2

For the handling of arrangements and objects I recommend you use Lodash , it is an excellent library that was based on other as Underscore to facilitate this task and today it is used in a variety of Frameworks, as well as it is very well maintained and has excellent documentation.

As for your case, you could do it very easily in the following way:

//Por ejemplo $scope.coffedate es igual a [{ 'id': 1 }, { 'id': 2 }];
$scope.coffedate = $scope.coffedate || []; //Validar que sea un arreglo
var newItem = { 'id': 1 }; //Nuevo elemento a agregar
$scope.coffedate = _.unionBy($scope.coffedate, [newItem], 'id'); //Donde id es el identificador

Greetings, Nicholls

    
answered by 23.01.2017 / 01:46
source
1

this $scope.coffedate = [];

should be up- out of $scope.realizarSuma = function(item,inicial){

Explanation:

But your arrangement is always empty, why do you assign [], $scope.coffedate = []; ,

then you want to add an element to your arrangement, therefore your arrangement will always have one element at most.

We'll see:

var numerico = item.numerico; //identificador
  var preciototal = precio*contador; //total de compra por producto
  $scope.coffedate = [];//declaracion de tu array vacio

  $scope.coffedate.push({total:preciototal,identificador:numerico})

  console.log($scope.coffedate);
    
answered by 23.01.2017 в 00:19