搜索对象关键字名称和使用功能



我有一个对象,我正试图在数组中推送它

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

}

所以现在我要做的是把条件。如果在attribute-set[1]中未找到_name": "letter-spacing",则推送属性数组中的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);
}
});

我哪里错了

你可以这样做

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

最新更新