我需要替换对象属性值中的字符。我正在遍历对象数组(输出到控制台)并检索 FeatureUrl 属性。
我有从 Svc 返回的该属性的数据,格式如下:
index.html#/blah
我需要将“#”替换为“#/app”,以便我的新网址以以下形式返回:
index.html#/app/blah
我不确定 .replace 是否是此处使用的正确方法,但这是我所看到的建议。有人可以指出我正确的方向吗?
var localFeatureDetails = function() {
$scope.user = userService.GetUserInformation();
$scope.featureDetails = $scope.user.Features;
var featureUrlRewrite = function () {
var index;
var urlCount;
for (index = 0; index < $scope.featureDetails.length; index++) {
urlCount = $scope.featureDetails[index];
urlCount.FeatureUrl.replace("#","#/app");
console.log(urlCount);
}
};
featureUrlRewrite();
};
localFeatureDetails();
最佳答案
我没有测试您的代码,但根据 .replace()
的工作原理,您必须通过覆盖它再次将值分配给您的对象属性,否则您不会保存该值。
假设其他一切都是正确的,试试这个:
$scope.featureDetails[index].FeatureUrl = urlCount.FeatureUrl.replace("#","#/app");
引用:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
关于javascript - 循环数组Javascript后替换对象属性值中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36014613/