在数组中获取数组

时间:2018-12-17 11:26:03

标签: javascript arrays angularjs json object

我有一个默认情况下得到的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"
    }

注意:-我正在对第二个attribute名称attribute-set的{​​{1}}进行操作

现在,由于pagenum是一个对象,因此我将其作为数组。我将attribute-set[1]-> attribute做成一个数组,因为我需要在其中推入更多对象。

attribute

现在它成功创建了一个数组:-

if(typeof $scope.ContentObj.stylesheet["attribute-set"][1].attribute === "object"){ //Check if object
                const content = $scope.ContentObj.stylesheet["attribute-set"][1].attribute; //Get the content
                $scope.ContentObj.stylesheet["attribute-set"][1].attribute = [content]; //Put the content in an array
            }

此后,我尝试通过检查是否存在$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" } 来推动对象。我可以通过以下代码在数组中成功推送此代码:-

_name

因此,现在,我得到像这样的结果//check color var checkContentPageColor = obj => obj._name === 'color'; //for checking font name var checkContentPageFont = obj => obj._name === 'font-family'; //check color in the attr json var checkContentPageColor_available = $scope.ContentObj.stylesheet["attribute-set"][1].attribute.some(checkContentPageColor); // check font family var checkContentPageFont_available = $scope.ContentObj.stylesheet["attribute-set"][1].attribute.some(checkContentPageFont); if( checkContentPageColor_available === false && checkContentPageFont_available === false ){ console.log('not available' ); $scope.ContentObj.stylesheet["attribute-set"][1].attribute.push({ "_name": "color", "__prefix": "xsl", "__text": "black" },{ "_name": "font-family", "__prefix": "xsl", "__text": "sans" } ); console.log("pushed successfully"); console.log($scope.ContentObj); } ,这是正确的。 :-

{ attribute: [{..},{..},{..}],something  }

此后,当我重新加载应用程序时,代码再次被推入以生成双数组。 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": "100" }, { "_name": "color", "__prefix": "xsl", "__text": "black" }, { "_name": "font-family", "__prefix": "xsl", "__text": "sans" } ], "_name": "pagenum", "__prefix": "xsl" } ], "_version": "2.0", "__prefix": "xsl" } :-

{ attribute: [[{..},{..},{..}],{..},{..}],something  }

我要去哪里错了?根据我的代码提出一些更改建议。很久以来我一直被困在这里。

3 个答案:

答案 0 :(得分:1)

如果我没看错,首先要检查对象是否是对象,然后将其包含的内容推入数组。重新加载后,您要检查数组是否是一个对象,这是正确的,因此您再次将数组的内容放到另一个数组中并推送内容的重复项。因此,您获得了以下内容:

{ attribute: [[{..},{..},{..}],{..},{..}],something }

而不是这样:

{ attribute: [{..},{..},{..},{..},{..}],something }

答案 1 :(得分:1)

问题是您试图区分“对象”和“数组”的方式-数组是对象

if(typeof $scope.ContentObj.stylesheet["attribute-set"][1].attribute === "object"){ ... } //Check if object

这始终是事实- 考虑一下:

const o = {"A": 1, "B": 2}
const l = [o]
const n = [1, 2]

typeof(o) //"object"
typeof(l) //"object"
typeof(n) //"object"

您可能想使用的是instanceof

o instanceof Array //false
l instanceof Array //true
n instance of Array //true

答案 2 :(得分:0)

您应该检查键“ _name”的值是否已经存在。这是一个小实用程序,它检查特定的属性_name是否存在,如果找不到属性_name,则将其自动推送到数组。

沙箱:https://codesandbox.io/s/o1vpkxv5w5

function addAtts(arr, att) {
    var found = arr.some(function (at) {
      return at._name === att._name;
    });
    if (!found) {
        arr.push(att);
    }
}

addAtts(object["attribute-set"][1].attribute, {
    "_name": "color",
    "__prefix": "xsl",
    "__text": "black"
});

addAtts(object["attribute-set"][1].attribute, {
    "_name": "font-family",
    "__prefix": "xsl",
    "__text": "sans"
});

console.log(object["attribute-set"][1]);

结果:

{ attribute:
   [ { _name: 'font-weight', __prefix: 'xsl', __text: 'bold' },
     { _name: 'color', __prefix: 'xsl', __text: 'black' },
     { _name: 'font-family', __prefix: 'xsl', __text: 'sans' } ],
  _name: 'pagenum',
  __prefix: 'xsl' }

让我们尝试再次运行它以推送相同的属性,但让我们添加一个新属性。

addAtts(object["attribute-set"][1].attribute, {
    "_name": "color",
    "__prefix": "xsl",
    "__text": "black"
});

addAtts(object["attribute-set"][1].attribute, {
    "_name": "font-family",
    "__prefix": "xsl",
    "__text": "sans"
});

addAtts(object["attribute-set"][1].attribute, {
    "_name": "font-family",
    "__prefix": "xsl",
    "__text": "sans"
});

console.log(object["attribute-set"][1]);

结果:

{ attribute:
   [ { _name: 'font-weight', __prefix: 'xsl', __text: 'bold' },
     { _name: 'color', __prefix: 'xsl', __text: 'black' },
     { _name: 'font-family', __prefix: 'xsl', __text: 'sans' },
     { _name: 'text-decoration',
       __prefix: 'xsl',
       __text: 'underline' } ],
  _name: 'pagenum',
  __prefix: 'xsl' }

请注意,“ $ scope.ContentObj.stylesheet”对象是沙箱中的变量“ object”。