Perform .join on value in array of objects

前端 未结 10 1522
囚心锁ツ
囚心锁ツ 2020-11-29 15:31

If I have an array of strings, I can use the .join() method to get a single string, with each element separated by commas, like so:

[\"Joe\", \"         


        
10条回答
  •  心在旅途
    2020-11-29 16:01

    lets say the objects array is referenced by the variable users

    If ES6 can be used then the easiest solution will be:

    users.map(user => user.name).join(', ');
    

    If not, and lodash can be used so :

     _.map(users, function(user) {
         return user.name;
     }).join(', ');
    

提交回复
热议问题