0

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?

1 Answer 1

2

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">
Sign up to request clarification or add additional context in comments.

2 Comments

<input type="text" ng-model="usermodel" /> and in angular you can assign as $scope.usermodel=users.map(function(el){return el.name}).join(",") @sreehari
$scope.userModel = users.map((e) => e.name).join(","); :)

Your Answer

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.