在没有数组的情况下读取并推送另一个对象中的对象 [英] Read and push object in another object without array

查看:95
本文介绍了在没有数组的情况下读取并推送另一个对象中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JSON检查并推送对象详细信息中的对象.

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

这就是我的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"
  }
}

现在,我正在尝试读取属性设置值[1] ,即; "pagenum" .在这里,我正在尝试检查具有名称的更多属性值.如果不存在,则将其推入该属性集中.

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.

我将 attribute-set [0 ]放入array并没有任何问题.在这里,我在 attribute-set [1]中得到了一个对象.

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].

将此属性用于attr-set [1],但抛出错误-Uncaught TypeError: $scope.contentObj.stylesheet.attribute-set[1].attribute.some is not a function

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

如果存在像 attribute-set [0] 这样的数组,我可以成功实现上述代码.如何检查单个对象.如果未找到,则push和一个数组将在 attribute-set [1] 吗?

推荐答案

在您的示例中,$scope.contentObj.stylesheet.attribute-set[0].attribute确实是一个数组,而$scope.contentObj.stylesheet.attribute-set[1].attribute是一个对象,该原型没有some()方法.我以为这只是一个错字,所以我将$scope.contentObj.stylesheet.attribute-set[1].attribute更改为一个数组,现在一切似乎都可以正常工作:

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

这篇关于在没有数组的情况下读取并推送另一个对象中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆