在数组中获取空​​值

时间:2019-02-04 11:20:44

标签: javascript arrays angularjs json

我有一个默认的JSON文件:-

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

我试图通过创建数组来推入一些值,但是在推入数据后得到数组 undefined

{
    "_name":"__tableframe__top",
    "_use-attribute-sets":"common.border__top",
    "__prefix":"xsl",
    "attribute":[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];
 }

此后,我正在检查数组中是否存在_name = something的属性。如果没有,则按。

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

我在数组上得到了空值,并且错误TypeError: Cannot read property '_name' of undefined at checkTableFrameTopColor

我要去哪里错了?

编辑:-

像我想要实现的那样-

{
    "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"
}

1 个答案:

答案 0 :(得分:1)

我不得不猜测,但让我将其发布为答案,以便可以使用格式…

给出:

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

注意:input.attribute的值未定义。

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

..因此,如果此输入是您在if语句中访问的内容

input.attribute instanceof Array => false

这将是正确的,并且您的代码块将被执行,并显示:

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

如果我对您的理解正确,则可以这样解决:

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

如果属性值可能为false,则必须使用tableframe__top_content === undefined ||进行检查。 tableframe__top_content ===空。