Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 搜索对象键名称和使用函数_Javascript_Arrays_Angularjs - Fatal编程技术网

Javascript 搜索对象键名称和使用函数

Javascript 搜索对象键名称和使用函数,javascript,arrays,angularjs,Javascript,Arrays,Angularjs,我有一个物体,我正试图把它推入一个数组中 $scope.xslLetterSpacing = { "_name": "letter-spacing", "__prefix": "xsl", "__text": "2pt" }; 因此,在我试图推入另一个数组的对象上方:- $sc

我有一个物体,我正试图把它推入一个数组中

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

所以现在我要做的就是把条件。当
\u name:“字母间距”
属性集[1]
中找不到时,请在属性数组中按
xslLetterSpacing
对象,如果具有
\u name:“字母间距”
的数据对象已存在于
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);
                    }
                });
我哪里做错了

你可以这样做

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

“attribute”是一个数组,你不需要遍历数组并测试数组中的每个对象吗?@splaten是的,我需要匹配数组中每个对象的
\u name
,但我不知道如何匹配。这个问题中没有JSON。这些是javascript对象,JSON是字符串notation@Liam我非常确定这些JSON是在javascript对象中定义的。不是吗?不,你错了,JSON是一个字符串,也就是说,
“{\'a\”:“test\”}
一个Javascript对象就是一个对象,也就是说,
{a:“test”}
我得到了错误
角度。js:13424错误:[ng:areq]参数控制器不是一个函数,这正是我要找的。