0

I am new in javascript and angular. Suppose I have an JSON object with {"xyz":{Key:info}} . I want to add {Key:info} in to an array .

I want to make "xyz" an array. Eg: {"xyz":[{Key:info}]} So that I can push more of {Key:info} into that array- {"xyz":[{Key:info},{Key:info},{Key:info}]}.

Also I need to check every time if xyz is object then make it array and push only once.

I am not getting how can I do this with angular javascript.

EDIT :-Added orig JSON

$scope.ContentObj= { "attribute-set": [ { "attribute": [ { "_name": "text-align", "__prefix": "xsl", "__text": "end" }, { "_name": "end-indent", "__prefix": "xsl", "__text": "10pt" } ], "_name": "odd__header", "__prefix": "xsl" }, { "attribute": { "_name": "font-weight", "__prefix": "xsl", "__text": "bold" }, "_name": "pagenum", "__prefix": "xsl" } ], "_version": "2.0", "__prefix": "xsl" } 

    3 Answers 3

    2

    You can use typeof to know if it is an object and then you can get its content and initialize an array with it

    let myObject = {"xyz":{Key:"info"}}; if(typeof myObject.xyz === "object"){ //Check if object const content = myObject.xyz; //Get the content myObject.xyz = [content]; //Put the content in an array } console.log(myObject);

    If you need to use it in your code as asked in the comments :

    if(typeof $scope.ContentObj.stylesheet["attribute-set"][4].xyz === "object"){ //Check if object const content = $scope.ContentObj.stylesheet["attribute-set"][4].xyz; //Get the content $scope.ContentObj.stylesheet["attribute-set"][4].xyz = [content]; //Put the content in an array } console.log(myObject); 
    9
    • Hi , thank you for the clean code. Now suppose I need to call information in this way let myObject = $scope.ContentObj.stylesheet["attribute-set"][4].xyz; , then in console I can see only object .
      – WhoAmI
      CommentedDec 17, 2018 at 7:01
    • Then replace myObject by $scope.ContentObj.stylesheet["attribute-set"][4] and leave the first line initializing the object
      – Weedoze
      CommentedDec 17, 2018 at 7:05
    • You mean to say the object let myObject = {"xyz":{Key:"info"}}; should be hard coded ?
      – WhoAmI
      CommentedDec 17, 2018 at 7:12
    • I am still confuse about the first line. let myObject . I have added my real JSON call in EDIT and I need to make it like first attribute-set. Call scope.ContentObj.stylesheet["attribute-set"][1]. Here you can see attribute-set[1].attribute is object.
      – WhoAmI
      CommentedDec 17, 2018 at 7:32
    • 1
      Just replace typeof myObject === .. by typeof $scope.ContentObj.stylesheet["attribute-set"][1] === ... and do the same for the following line. You also have to delete the first line let myObject = .... I see that you are missing the basics of javascript. You should first read tutorial about basic javascript
      – Weedoze
      CommentedDec 17, 2018 at 7:55
    2

    This might be what you are asking.

    if (!angular.isArray($scope.yourObject.xyz)){ $scope.yourObject = { xyz: [] } } $scope.yourObject.xyz.push({Key:'info'}) 
    2
    • I did that , but now somehow my array is getting pushed twice , Example {"xyz":[[{Key:info},{Key:info},{Key:info}],{Key:info},{Key:info}]}
      – WhoAmI
      CommentedDec 17, 2018 at 6:48
    • But i think , this code will create array whenever the controller will get load each time. I want to find object only once . And if array is there then skip.
      – WhoAmI
      CommentedDec 17, 2018 at 6:52
    1

    Try this way.

    $scope.yourObject.xyz.push(Object.assign([], yourObject)) 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.