Problems adding item to array with Angularjs

Asked

Viewed 1,954 times

6

Carrinho de comprar

I’m putting together a shopping cart with dynamic options with Angularjs.

The problem is here:

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

I need that when I add the optional array, that the quantity joins it, being like this:

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

This is the ticket of the option:

$scope.ck = function(id) {
    var oidsa = {"3": 1};
    $scope.user.roles[id].push(oidsa);
    alert($scope.user.roles[id]);
};
  • 2

    Edit your question and add the code you are using.

3 answers

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 );

1

The estate push only works for arrays, for objects it does not assign values. In this case, assume that your collection is an object with two indexes 14 and 15 and within one of these indices there is a layer array index 0, containing an object with 3 attributes, and that the data input for the method is a id. Let’s assume that the parameter passed in the method is your index 14 (of 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, so that the object of this array of index 0 does not remain undefined (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 will also be its values and that its object will be used in the same way in its sequence, as it was defined in its first instance of creation of the attribute. And for this it is necessary to pass some value, even if it is null (but this is at your discretion, since it is not the problem nor the doubt for your question at the time):
$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]); 
  }
};

1

If you have a guarantee that each item will be a list containing the details of the item in index 0 and the quantity in index 1, you can iterate between the keys of the quantity object 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];
      }
    }
  }
}

Remember 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 this data.

Browser other questions tagged

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