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

9 Comments

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 .
Then replace myObject by $scope.ContentObj.stylesheet["attribute-set"][4] and leave the first line initializing the object
You mean to say the object let myObject = {"xyz":{Key:"info"}}; should be hard coded ?
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.
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
|
2

This might be what you are asking.

if (!angular.isArray($scope.yourObject.xyz)){
    $scope.yourObject = {
        xyz: []
    }
}
$scope.yourObject.xyz.push({Key:'info'})

2 Comments

I did that , but now somehow my array is getting pushed twice , Example {"xyz":[[{Key:info},{Key:info},{Key:info}],{Key:info},{Key:info}]}
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.
1

Try this way.

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

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.