【问题标题】:Search object key name and use function搜索对象键名和使用功能
【发布时间】:2018-11-21 10:09:05
【问题描述】:

我有一个对象,我想把它推入一个数组

$scope.xslLetterSpacing = {
                            "_name": "letter-spacing",
                            "__prefix": "xsl",
                            "__text": "2pt"
                          };

所以在我试图推入另一个数组的对象之上:-

$scope.frontObjct = {
"stylesheet": {
    "attribute-set": [{
            "attribute": {
                "_name": "text-align",
                "__prefix": "xsl"
            },
            "_name": "__frontmatter",
            "__prefix": "xsl"
        },
        {
            "attribute": [{
                    "_name": "space-before",
                    "__prefix": "xsl"
                },
                {
                    "_name": "space-before.conditionality",
                    "__prefix": "xsl",
                    "__text": "retain"
                },
                {
                    "_name": "font-size",
                    "__prefix": "xsl",
                    "__text": "16pt"
                },
                {
                    "_name": "font-weight",
                    "__prefix": "xsl",
                    "__text": "500"
                },
                {
                    "_name": "line-height",
                    "__prefix": "xsl",
                    "__text": "90%"
                }
            ],
            "_name": "__frontmatter__title",
            "_use-attribute-sets": "common.title",
            "__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"
}

}

所以现在我要做的是设置条件。当_name": "letter-spacing" notattribute-set[1] 中找到时,然后将 xslLetterSpacing 对象推送到属性数组中,如果 _name": "letter-spacing" 的数据对象已经存在于 Obj 中,则执行什么都没有。

我尝试这样做是为了重复数组中已经存在的对象数量。

angular.forEach($scope.frontObjct.stylesheet["attribute-set"][1].attribute , function(value, key) {
                    if (key !== "_name") {
                    //pushing extra data to attribute field
                        $timeout(function(){
                          $scope.frontObjct.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
                            console.log($scope.frontObjct );
                        },500);
                    }
                });

我哪里错了?

【问题讨论】:

  • 'attribute'是一个数组,不需要遍历数组,测试数组中的每个对象吗?
  • @SPlatten 是的,我需要匹配数组中每个对象的 _name,但我不知道如何。
  • 这个问题没有 JSON。这些是 javascript 对象,JSON 是一个字符串表示法
  • @Liam 我很确定这些 JSON 是在 javascript 对象中定义的。不是吗?
  • 没有错,JSON 是一个 字符串"{\"a\":\"test\"}" Javascript 对象是一个对象,即 {a:"test"}

标签: javascript arrays angularjs


【解决方案1】:

你可以这样做

var itemLetterSpacing = $scope.Obj.stylesheet["attribute-set"][1].attribute.filter(function(item) {
      return item._name === "letter-spacing";
})[0];

if(!itemLetterSpacing) { 
     $scope.Obj.stylesheet["attribute-set"][1].attribute.push($scope.xslLetterSpacing);
}
console.log($scope.Obj);

【讨论】:

  • 我收到错误angular.js:13424 Error: [ng:areq] Argument controller is not a function
  • 现在复制试试
  • 谢谢,这正是我要找的。​​span>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多