Aniruth
Aniruth

Reputation: 187

Json Object push not working using AngularJS

I have a data with certain rule. I want to create a json object to manage the rule. There is problem to create a json object as my need. Here my array data.

 $scope.data = ["Crust^Pan^Medium=NA", "Crust^Pan^Large=NA", "Crust^Thin Crust^Medium=10.50"]

I want a output like this:

  {
     "Pan": {
             "Medium": NaN,
             "Large": NaN,
          },
    "Thin Crust": {
             "Medium": 10.50
          }
   }

Here my code,

   $scope.crustRule = {};


    for(var i=0; i<$scope.data.length; i++) {
        var tempCrust = {};                        
        var trimOne = $scope.data[i].split('^');
        var trimTwo = trimOne[2].split('=');
        if(trimOne[0] == 'Crust') {
           tempCrust[trimTwo[0]]=parseFloat(trimTwo[1]);
           $scope.crustRule[trimOne[1]].push(tempCrust);
        }
    }
    console.log($scope.crustRule);

Upvotes: 1

Views: 765

Answers (2)

Sarfaraaz
Sarfaraaz

Reputation: 488

the push function has to exist. you can grab it from the Array property if you want. only do this if it has to be in an object structure

var x = {length:0,push:Array.prototype.push};
x.push("jump");
console.log(x);//Object {0: "jump", length: 1}

I go over the mininmum requirement for some array functions to work on an object:

Mimic the structure of a javascript array object

EDIT: I noticed your reuirements are need an object without a length and string index based instead of number index based. going to test something

darn I was hoping something wild was already there and tried

var x = {};
x += {"BadGuy": "Joker"};
console.log(x)//[object Object][object Object] //:(

so I made my own push function

var x = {push:ObjPush};
x.push("jump");//Object cannot add (string) yet Coming soon
y = {"BadGuy": "Joker"};
x.push(y);
console.log(x);//{"BadGuy": "Joker"};

function ObjPush(obj)
{
    if ((typeof obj).toLowerCase() == "object")
    {
        for (var i in obj)
        {
            this[i] = obj[i];
        }
    }
    else
    {
        console.log("Object cannot add (" + typeof obj + ") yet\n Coming soon");
    }
}

Note: I haven't added any handling to check for same properties. so any properties with the same name will override original properties.

EDIT: I integrated my code with yours and got a strange output unfortunately. for some reason instead of adding medium and large as properties to the inner objects it only adds the last 1 for example i get the output

{"Pan":{"Large":null},"Thin Crust":{"Medium":10.5}}

EDIT: OK I found where my issue was. I get the expected output now. added a check to make sure that $scope.crustRule[trimOne[1]] is only initialized if it doesnt exist yet.

if(typeof $scope.crustRule[trimOne[1]] == "undefined")
    $scope.crustRule[trimOne[1]] = {push:ObjPush};

Upvotes: 1

Felix
Felix

Reputation: 10078

You first need to create an object $scope.crustRule[trimOne[1]] before you can push objects into it. Something like

$scope.crustRule[trimOne[1]] = {};
$scope.crustRule[trimOne[1]].push(tempCrust);

Upvotes: 1

Related Questions