0

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"
Ian G
5,5935 gold badges54 silver badges76 bronze badges
asked Jan 19, 2016 at 10:57
3
  • 9
    myArray.join(",") Commented Jan 19, 2016 at 10:58
  • 3
    This has nothing to do with AngularJS. That's something you can do purely using JavaScript Commented Jan 19, 2016 at 10:59
  • Thanks yes Its working @Alnitak Commented Jan 19, 2016 at 11:15

2 Answers 2

3

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;

}

answered Jan 19, 2016 at 11:06
Sign up to request clarification or add additional context in comments.

Comments

0

Try to use join() as follows

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

answered Jan 19, 2016 at 12:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.