Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have an array of JSON objects in the format

users=[{id:'1',name:'ABC'},{id:'2',name:'DEF'},............]

I need to display the names of all users in an input text field comma separated as below image.

enter image description here

I tried using ng-list directive, but for that I had to first loop through the user objects and store all the names in a separate array and use that array as ng-model for the <input> element. Is there an easy, alternate way in angularjs?

share|improve this question
up vote 2 down vote accepted

A simple JS solution Use Map function to get the desired property in array then use a join over it

 var users=[{id:'1',name:'ABC'},{id:'2',name:'DEF'}];

 $scope.userModel = users.map(function(el){return el.name}).join(",");

 <input type="text" ng-model="userModel">
share|improve this answer
1  
<input type="text" ng-model="usermodel" /> and in angular you can assign as $scope.usermodel=users.map(function(el){return el.name}).join(",") @sreehari – geekbro 6 hours ago
    
$scope.userModel = users.map((e) => e.name).join(","); :) – impregnable fiend 6 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.