避免重复执行数组推送

时间:2018-12-06 08:48:25

标签: arrays angularjs json

我创建了一个对象,并且在对象内部创建了一个数组,在其中推送了一个来自json数据的对象。

$scope.pagenumArr = {"attribute":[],"_name":"pagenum","__prefix":"xsl"};
            if ($scope.pagenumArr.attribute.indexOf($scope.contentObj.stylesheet["attribute-set"][4].attribute) == -1) {
                $scope.pagenumArr.attribute.push($scope.contentObj.stylesheet["attribute-set"][4].attribute);
            }
            console.log($scope.pagenumArr);

scope.contentObj.stylesheet["attribute-set"][4].attribute是我要推送的数据

{
  "_name": "font-weight",
  "__prefix": "xsl",
  "__text": "bold"
}

我正在用新对象替换旧对象

//replace old pagenum object with new array
            $scope.contentObj.stylesheet["attribute-set"][4] = $scope.pagenumArr;
            console.log($scope.contentObj);

因此,当我重新加载应用程序时,在数组"attribute":[]内创建了另一个数组。我只需要数组中的上一个push对象。如何防止重复执行代码?我要去哪里错了?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用

if(angular.isUndefined(nameofobject.dataPath[KeyOfData])){
   nameofobject.dataPath = KeyOfData;
}
相关问题