Problems adding item to array with AngularJS

6

I'mbuildingashoppingcartwithdynamicoptionswithAngularjs.

Theproblemishere:

{ "14": [ { "id": 3, "forma": "Alface", "preco": 1 }, { "3": 1 } ], "15": [ { "id": 3, "forma": "Alface", "preco": 1 }, { "3": 1 } ] }

I need when I add the optional array, that quantity joins it, thus:

{
    "14": [
        {
            "3": 1,
            "id": 3,
            "forma": "Alface",
            "preco": 1
        }
    ],
    "15": [
        {
            "3": 1,
            "id": 3,
            "forma": "Alface",
            "preco": 1
        }
    ]
}

This is the option code:

$scope.ck = function(id) {
    var oidsa = {"3": 1};
    $scope.user.roles[id].push(oidsa);
    alert($scope.user.roles[id]);
};
    
asked by anonymous 26.02.2015 / 04:49

3 answers

1

If you have a guarantee that each item will be a list containing the item's details at index 0 and the quantity at index 1, you can iterate between the quantity object keys and insert them into the first object. Something like:

var items = {
  "14": [
    {
      "id": 3,
      "forma": "Alface",
      "preco": 1
    },
    {
      "3": 1
    }
  ],
  "15": [
    {
      "id": 3,
      "forma": "Alface",
      "preco": 1
    },
    {
      "3": 1
    }
  ]
};

var quantidades;
var quantidadesKey;

// Itera entre as chaves do objeto de items
for (var id in items) {
  // Verificação de segurança
  if (items.hasOwnProperty(id)) {
    quantidades = items[id][1];

    // Itera entre as chaves do objeto de quantidades
    for (quantidadesKey in quantidades) {
      // Verificação de segurança
      if (quantidades.hasOwnProperty(quantidadesKey)) {
        // Atribui o valor da quantidade na chave
        // correspondente do objeto de items
        items[id][0][quantidadesKey] = quantidades[quantidadesKey];
      }
    }
  }
}

Remembering that this works only if your object has exactly the format you reported. And more importantly, there should be a better solution if you can improve the structure of these data.

    
19.03.2015 / 05:26
1

The push property only works for arrays, for objects it does not assign values. In this case, assume that your collection is an object with two 14 and 15 indexes and within one of these indexes there is an array strong> 0 , which contains an object with 3 attributes, and that the data entry for the method is an id . Let's assume that the parameter passed in the method is your index (from the first level of your collection):

$scope.collection = {
    "14": [
        {
            "id": 3,
            "forma": "Alface",
            "preco": 1
        }
    ],
    "15": [
        {
            "id": 3,
            "forma": "Alface",
            "preco": 1
        }
    ]
}
$scope.ck = function(id) {
    $scope.collection[id][0]["3"] = 1;
     console.log($scope.collection[id]);
};
However, in order for the object of this array of index 0 not to be undefined, since it is expected to have the same attributes, when it comes to object, when creating attributes to the object of the collection, it is expected that there are also their values and that its object be used in the same way in its sequence, as defined in its first instance of creating the attribute. And for this it is necessary to pass some value, even if it is null (but this is in your discretion, since it is neither the problem nor the doubt for your question at the moment):
$scope.ck = function(id) {
  if ($scope.collection.length > 0) {
      angular.forEach($scope.collection, function(key, value) {
        $scope.collection[key][0]["3"] = (key == id) ? 1 : null;
      }
    console.log($scope.collection[id]); 
  }
};
    
03.08.2015 / 18:54
1

Use one of the codes below:

$scope.user.roles[id]['3'] = 1;

or

var oidsa = {"3": 1};
angular.extend( $scope.user.roles[id], oidsa );
    
10.12.2015 / 12:20