如何在Javascript

时间:2016-01-19 10:57:20

标签: javascript

我有一个数组$scope.userDays,如下所示:

$scope.userDays = [2,3,4,5,6];

只需要取值并将它们转换为字符串。期望的输出将是这样的:

$scope.userDays ="2,3,4,5,6"

2 个答案:

答案 0 :(得分:3)

在Javascript中Join()用于将数组转换为字符串。你应该试试这个:

    $scope.userDays = $scope.userDays.join() ;

如果上述方法不起作用,那么您应该尝试以下功能

    function createStringByArray(array) {
        var output = '';
        angular.forEach(array, function (object) {
            angular.forEach(object, function (value, key) {
            output += key + ',';
            output += value + ',';
    });
});
return output;

}

答案 1 :(得分:0)

尝试使用join(),如下所示

var userDays = [2,3,4,5,6];
userDays = userDays.join(',');
alert(userDays);