0

I am trying to check and push object in an object details from a JSON.

This is how my JSON look like

{
  "stylesheet": {
    "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"
      }
      ],
    "_xmlns:xsl": "http://www.w3.org/1999/XSL/Transform",
    "_xmlns:fo": "http://www.w3.org/1999/XSL/Format",
    "_version": "2.0",
    "__prefix": "xsl"
  }
}

Now , I am trying to read the attribute-set value [1] ie; "pagenum" . HEre I am trying to check for more attribute value with name. If not present push it into that attribute set.

I don't have any problem in pushing into attribute-set[0] as it is in array . Here I got single object in attribute-set[1].

Tried this for attr-set[1], but throwing error - Uncaught TypeError: $scope.contentObj.stylesheet.attribute-set[1].attribute.some is not a function

//for checking font name
            var checkcontentPageFont = obj => obj._name === 'font-family';
            // check font family
            var checkcontentPageFont_available = $scope.contentObj.stylesheet["attribute-set"][1].attribute.some(checkcontentPageFont);

            if(checkcontentPageFont_available  === true ){
            }
            else{
                 $scope.contentObj.stylesheet["attribute-set"][1].attribute.push({
                            "_name": "font-family",
                            "__prefix": "xsl",
                            "__text": "sans"
                          }
                          );                
            }

I can successfully implementing the above code if there is an array like attribute-set[0]. How can I check this for single object. If not found push and an array will be created on attribute-set[1] ?

1 Answer 1

0

In your example $scope.contentObj.stylesheet.attribute-set[0].attribute is indeed an array, while $scope.contentObj.stylesheet.attribute-set[1].attribute is an object, which prototype does not have some() method. I assume it is just a typo, so I changed $scope.contentObj.stylesheet.attribute-set[1].attribute to be also an array and now everything seems to work properly:

const data = {
  "stylesheet": {
    "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"
          }
        ]  
      }
    ],
    "_xmlns:xsl": "http://www.w3.org/1999/XSL/Transform",
    "_xmlns:fo": "http://www.w3.org/1999/XSL/Format",
    "_version": "2.0",
    "__prefix": "xsl"
  }
}

const checkcontentPageFont = obj => obj._name === 'font-family';

const checkcontentPageFont_available = data.stylesheet["attribute-set"][1].attribute.some(checkcontentPageFont);

if (!checkcontentPageFont_available) {
  data.stylesheet["attribute-set"][1].attribute.push({
    "_name": "font-family",
    "__prefix": "xsl",
    "__text": "sans"
  });  
}

console.log(data);

Sign up to request clarification or add additional context in comments.

2 Comments

Have you modified the JSON manually ? If, so then it's wrong because the JSON is generating from other source. Bdw I have solved this issue. Thanks for you answer
Yes, I modified manually, assuming that both stylesheet.attribute-set[0].attribute and stylesheet.attribute-set[1].attribute should be of the same format: arrays. In your question the firs is array, but the second one is an object.

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.