1

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);
3
  • are you getting an error? if not what are you getting? and what does the question have to do with angular? Commented May 26, 2015 at 7:37
  • i get a error in my console of Cannot read property 'push' of undefined Commented May 26, 2015 at 7:39
  • heh! which makes the answer pretty obvious... Commented May 26, 2015 at 7:40

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

6 Comments

the push method should only exist in array like objects. but $scope.crustRule[trimOne[1]] is an empty object. this question might help
again error print in console $scope.crustRule[trimOne[1]].push is not a function
as @Sarfaraaz, push should be use with arrays; not so much with objects. you can define $scope.crustRule[trimOne[1]] as arrays, or just use assignment instead of push: $scope.crustRule[trimOne[1]][trimTwo[0]]=parseFloat(trimTwo[1])
not getting proper answer. Loop ever execute, to declare new array to clear old values.
@Aniruth I think you need to set the templated variable outside of the loop
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.