Ambika KU
Ambika KU

Reputation: 89

how to convert Array to String in Javascript

I have an array $scope.userDayslooking like this:

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

need to take only the values and convert them into a string. The desired output would be something like this:

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

Upvotes: 0

Views: 78

Answers (2)

Shijin TR
Shijin TR

Reputation: 7768

Try to use join() as follows

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

Upvotes: 0

Tayyab Ahmed
Tayyab Ahmed

Reputation: 83

In Javascript Join() is use to convert array into string. You should try this:

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

If the above doesnot work then you should try the below function

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

}

Upvotes: 3

Related Questions