【发布时间】:2019-05-07 22:14:20
【问题描述】:
我创建了一个对象,并在对象内部创建了一个数组,我在其中推送一个来自 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":[] 中创建另一个数组。我只需要数组中的前一个推送对象。如何防止在我的代码中重复推送?我哪里错了?
【问题讨论】: