javascript - 将对象推送到数组时,新行会附加在末尾

标签 javascript arrays angularjs json object

我的 html 中有一个选择输入和输入

   <select class="span3 align-right-input" ui-select2="{minimumResultsForSearch: -1}" ng-model="info.otherUse" ng-init="info.otherUse=lists.primaryFunctions[0].typeName">
            <option ng-repeat="list in lists.primaryFunctions">
                {{list.typeName}}
                </<option>

          </select>

   <input type="number" name="otherGfa" ng-model="info.otherGfa" />

在我的 Controller 中,我试图将其插入这样的对象数组中。

 $scope.otheruses.push({'use':$scope.info.otherUse,'gfa':$scope.info.otherGfa});

当我对其他用途进行控制台日志时。这就是我得到的

[…] 0: {…} gfa: 40 use: "\n Ambulatory Surgical Center\n \n\n " proto: Object { … } length: 1 proto: Array []

我无法弄清楚为什么要附加这些新行字符\n 。当我在 View 中显示它时,这会导致问题,所以我想删除它们。

提前致谢:-) :-)

最佳答案

不知道为什么 \n 首先存在,很可能它已经存在于您引用的源字段中。您应该检查 $scope.info.otherUse 的内容。

至于摆脱它,这里有一个正则表达式:

$scope.info.otherUse.replace(/\n/g, '')

关于javascript - 将对象推送到数组时,新行会附加在末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49031451/

相关文章:

java - 将 2 个 String[][] 和 1 个 String[] 合并为一个 String[][] 时出错

javascript - Angular 异步http请求

javascript - 使用 Handlebars 生成 JSON

javascript - 使用javascript检查服务器上是否存在html文件

javascript - 为什么要使用 JSON.parse(decodeURIComponent(staticString))?

javascript - 如何获取数组中的所有唯一对象(具有两个值)?

arrays - 在 bash 中搜索数组返回索引

PHP:将数字设置为关联数组中的键

javascript - Angular Click 中的多重表达式

angularjs - $http 拦截器导致无限循环