0

I have a default JSON file :-

{
    "_name":"__tableframe__top",
    "_use-attribute-sets":"common.border__top",
    "__prefix":"xsl"
}

I am trying to push some value by creating an array , but I am getting my array undefined value after pushing the data

{
    "_name":"__tableframe__top",
    "_use-attribute-sets":"common.border__top",
    "__prefix":"xsl",
    "attribute":[undefined]
}

First I am checking if the object contains array or if not then create the array. And if if already an array is there then do nothing.

    if(!($scope.tObj.stylesheet["attribute-set"][4].attribute instanceof Array)){
        const tableframe__top_content = $scope.tObj.stylesheet["attribute-set"][4].attribute;
        $scope.tObj.stylesheet["attribute-set"][4].attribute = [tableframe__top_content];
 }

After this , I am checking if attribute with _name = something is already there or not in the array. If not then push.

var checkTableFrameTopColor = obj => obj._name === 'border-before-color';
        var checkTableFrameTopWidth = obj => obj._name === 'border-before-width';

        var checkTableFrameTopColor_available = $scope.tObj.stylesheet["attribute-set"][4].attribute.some(checkTableFrameTopColor);

        var checkTableFrameTopWidth_available = $scope.tObj.stylesheet["attribute-set"][4].attribute.some(checkTableFrameTopWidth);

        if( checkTableFrameTopColor_available === false && checkTableFrameTopWidth_available  === false ){
            $scope.tObj.stylesheet["attribute-set"][4].attribute.push({
                    "_name": "border-before-color",
                    "__prefix": "xsl",
                    "__text": "black"
                    },{
                    "_name": "border-before-width",
                    "__prefix": "xsl",
                    "__text": "1pt"
                     }
                     );
            console.log("pushed successfully");     
            console.log($scope.tObj);       
        }

I am getting null value on array and an error TypeError: Cannot read property '_name' of undefined at checkTableFrameTopColor.

Where am I going wrong ?

EDIT:-

Like this I want to achieve-

{
    "attribute":[
                    {"_name":"font-weight","__prefix":"xsl","__text":"bold"},
                    {"_name":"color","__prefix":"xsl","__text":"black"}
                ],
    "_name":"__tableframe__top",
    "_use-attribute-sets":"common.border__top",
    "__prefix":"xsl"
}
5
  • some one told me to use square brackets just now may help? my question just now Commented Feb 4, 2019 at 11:28
  • go to the developer tools of your browser and check the value of obj Commented Feb 4, 2019 at 11:32
  • Try creating a JSFiddle with your code and post the link here Commented Feb 4, 2019 at 11:32
  • @zip No , in the above code if "attribute": { }is an object and is present in the main json object then it will turn to array. But there attribute is not present from begining Commented Feb 4, 2019 at 11:32
  • @FakharAhmadRasul I have already mentioned in the question. Getting { "_name":"__tableframe__top", "_use-attribute-sets":"common.border__top", "__prefix":"xsl", "attribute":[undefined] } Commented Feb 4, 2019 at 12:00

1 Answer 1

1

I have to guess but let me post it as an answer so I can use formatting…

Given:

const input = {
    "_name":"__tableframe__top",
    "_use-attribute-sets":"common.border__top",
    "__prefix":"xsl"
}

Note: Value of input.attribute is undefined.

    if(!($scope.tObj.stylesheet["attribute-set"][4].attribute instanceof Array)){
        const tableframe__top_content = $scope.tObj.stylesheet["attribute-set"][4].attribute;
        $scope.tObj.stylesheet["attribute-set"][4].attribute = [tableframe__top_content];
 }

.. so if this input is what you access in your if statement

input.attribute instanceof Array => false

it will be true and your code block will be executed and it says:

const example.attribute = [input.attribute]
// example.attribute == [undefined]

If I understand you correctly, you could solve it like so:

$scope.tObj.stylesheet["attribute-set"][4].attribute = (!tableframe__top_content) 
    ? [] 
    : [tableframe__top_content];

If attribute value could be false, you'd have to check with tableframe__top_content === undefined || tableframe__top_content === null.

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

3 Comments

Your logic is correct. Even I thought so but it is showing the same error. You mean like this if(!($scope.tableObj.stylesheet["attribute-set"][4].attribute instanceof Array => false)) ?
I updated the answer and added a suggested solution (before I just described why you see this error).
Ok , Yes I got it. Thanks for the solution. It works .

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.