我有一个阵列.我想:,\n为该数组中的每个元素添加一些字符()以显示在文本框中.
目前这就是我正在做的事情
$scope.source.arr = .... //This is an array
var actualText = "";
function func() {
    $scope.source.arr.forEach(function(ele){
        actualText += "***" + ele + " - \n"; //Adding necessary characters
    })
}
var showText = function() {
    func(); //Calling the function that populates the text as needed
    var textBox = {
          text : actualText; 
          ...
     }
}
有一个更好的方法吗?
您可以简单地使用Array.prototype.map创建一个包含更改字符串的新Array对象,如下所示
var textBox = {
      text: $scope.source.arr.map(function(ele) {
          return "***" + ele + " -  ";
      }).join("\n"),
      ...
 };
对于每个元素arr,我们创建一个与之对应的新字符串并创建一个字符串数组.最后我们加入数组中的所有字符串\n.