【发布时间】:2019-02-16 17:05:32
【问题描述】:
我正在使用 Angular javascript 来完成一项任务,我需要在点击函数时从数组中更新 JSON 键值。
我有这样的 JSON 结构:-
$scope.jsonObj = {
"stylesheet": {
"attribute-set": [{
"attribute": {
"_name": "text-align",
"__prefix": "xsl",
"__text": "center"
},
"_name": "__frontmatter",
"__prefix": "xsl"
},
{
"attribute": [{
"_name": "space-before",
"__prefix": "xsl",
"__text": "80mm"
},
{
"_name": "line-height",
"__prefix": "xsl",
"__text": "140%"
}
],
"_name": "__frontmatter__title",
"_use-attribute-sets": "common.title",
"__prefix": "xsl"
}
],
"_version": "2.0",
"__prefix": "xsl"
}
};
我有一个数组 $scope.textvalue=["center", "80mm","150%"] 。所以这里我要根据索引更新JSON的键值__text。表示我想根据 JSON 和数组中__text 的索引推送数组详细信息。
我在 controller 中单击按钮时执行此操作。
$scope.save = function(index) {
$scope.textvalue[index];
console.log($scope.textvalue);
$scope.objIndex = $scope.jsonObj.findIndex((obj => obj.__text));
console.log("Before update: ", $scope.jsonObj[$scope.objIndex]);
$scope.jsonObj[$scope.objIndex].__text = ? ? ? ;
console.log("After update: ", $scope.jsonObj[$scope.objIndex]);
}
我做了$scope.jsonObj[$scope.objIndex].__text = ???;因为我不知道该怎么做,我有一个错误,因为 $scope.jsonObj.findIndex is not a function
建议我用一些方法来更新我的 JSON 值。
【问题讨论】:
-
使用 jsonObj.stylesheet['attribute-set'] 更新值
-
@HimanshuShekhar 我在这里怎么办
$scope.jsonObj[$scope.objIndex].__text = ? ? ? ;我没听懂你
标签: javascript arrays angularjs loops object