我的API端点返回此JSON,我可以在浏览器中验证它
[1, 4686, 4691, 4696, 4]
我的问题是我在Angular中像这样运行$ http.get
return $http.get('url').then(function(response) {
//do some stuff here
});
我的回答变成了这个
["1", "4686", "4691", "4696", "4"]
我可以通过运行循环和parseInt每个元素来克服这个问题,但我想避免在我的脚本中使用太多的循环。这是什么原因以及如何解决?
PS:我已清除所有缓存并尝试使用Chrome Incognito,但仍无效。
答案 0 :(得分:1)
您可以快速映射以将所有字符串转换为数字:
return $http.get('url').then(function(response) {
$scope.numbers = response.map(Number);
});
如果您想快速验证这一点,请使用chrome:
进入控制台 ["1", "4686", "4691", "4696", "4"].map(Number)