genericuser
genericuser

Reputation: 1450

Array to string angular

New to angular. Please excuse me if this is a very simple question.

Is there anyway to convert array to string. My controller her the code:

$scope.addRow = function addRow(){     
    $scope.filters.push({ 'Name':$scope.name, 'dept': $scope.dept, 'city':$scope.city});
    $scope.name='';
    $scope.dept='';
    $scope.city='';
};

Now, I want to sent this data to a service as a string

I want it in the format "name,dept,city;name,dept,city". Is there any way to do it?

Service code looks like this:

                $scope.submit = function() {
                   myService.submit({
                      filters :$scope.filters
                 }, function(response) {
                    $scope.response = response; 
                });     
            };

I want to pass the value to filters in format "name,dept,city;name,dept,city" from the array. Thanks in advance.

Upvotes: 1

Views: 28481

Answers (1)

Christoph
Christoph

Reputation: 2053

You can use

angular.toJson(array);

to create a json String of your array.

Edit:

var array = [{name: 'misko', dept: '007', city: 'new york' }, {name: 'misko', dept: '007',city: 'new york'}];

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

var string = createStringFromArray(array);

Upvotes: 6

Related Questions