0

I have a template of json that I populate with data - in this case product data. For example I have the following json

// product template  
$scope.productAttributes = {
        "Code": null,
        'Attributes':
        {}
    };

When a user enters some product details through a Ui and calls loadPrices() I then populate productAttributes with this function...

    var loadPrices = function () {
    $scope.entity = {// grabs variables from a form};
        $scope.productAttributes.Code = $scope.productID.toUpperCase();
        $scope.productAttributes.Attributes = $scope.entity;
        $scope.productAttributes.Attributes.Term = $scope.productAttributesObj.Term;
        $scope.productAttributes.Attributes.Quantity = 1;          
    };

and this is the result to productAttributes...

{

"Code": "CON7",    
"Attributes": {        
    "Postcode": "n44er",        
    "rSize": 1000,        
    "Bandwidth": 10,        
    "Term": "36",        
    "Quantity": 1    
}
}

So my problem is that productAttributes is overwritten every time I try to add to productAttributes when I want to add new data by calling loadPrices. I am hoping to creat a structure like this...

{

"Code": "CON7",    
"Attributes": {        
    "Postcode": "n44er",        
    "Size": 1000,        
    "Bandwidth": 10,        
    "Term": "36",        
    "Quantity": 1    
},
"Code": "CON34",    
"Attributes": {        
    "Postcode": "nww45",        
    "Size": 10,        
    "Bandwidth": 10,        
    "Term": "36",        
    "Quantity": 1    
},
"Code": "CON89",    
"Attributes": {        
    "Postcode": "sw23ed",        
    "Size": 101,        
    "Bandwidth": 101  
}
}

Any ideas how I can achieve this? Any advice would be appreciated.

Also, I would like to create an ID field in each "Attributes" object for example ("ID": "9e5670fa-2fd7-4858-a667-c99cb5baf0f9"). Is it posible to create guids with javascript of Angular? Many thanks

Jose the hose
  • 1,805
  • 9
  • 33
  • 56
  • Your JSON doesn't look valid in that you have the same key multiple times in the same object. That is why you are seeing your values overwritten. – Matthew Green Feb 18 '15 at 17:49

2 Answers2

1

Make the variable an array and push objects into the array.

$scope.productAttributes = [];

var loadPrices = function () {
  var item = {
    code: $scope.productID.toUpperCase();
    attributes: {
      term: $scope.productAttributesObj.Term,
      quantity: 1
    }
  }

  $scope.productAttributs.push(item);       
};

you can make your item object be whatever you need it to be, i dont know how you have everything coming in. But pushing those items into the array will give you what you want.

ribsies
  • 1,218
  • 2
  • 10
  • 16
1

Try this:

var loadPrices = function () {
    $scope.entity = {/* grabs variables from a form*/};
    var myEntity = {};
    myEntity.Code = $scope.productID.toUpperCase();
    myEntity.Attributes = $scope.entity;
    myEntity.Attributes.Term = $scope.productAttributesObj.Term;
    myEntity.Attributes.Quantity = 1;         

    $scope.productAttributes.push(myEntity);
};

And don't forget to declare $scope.productAttributes as Array

$scope.productAttributes = [];

btw, more information about json manipulations: Adding/removing items from JSON data with JQuery

Community
  • 1
  • 1
jbigman
  • 1,200
  • 11
  • 24