3

Is there a way using lodash or another library to join an array of objects?

I'm looking for a readymade function not a for loop.

For example:

[{a: 1}, {a:3}, {a: 4}]
      //Run a function by specifing the property a and setting "," as the delimeter
Get 1,3,4

2 Answers 2

10

Here is your lodash answer

var arr = [{a: 1}, {a:3}, {a: 4}];
var s = _.map(arr, 'a').join(',');
//s == '1,2,3,4'
Sign up to request clarification or add additional context in comments.

Comments

6

You don't need lodash for this, you can just use map and join:

let collection = [{a: 1}, {a:3}, {a: 4}];
alert(collection.map(item => item.a).join(','));

2 Comments

hehe ecma script... lucky man since you are allowed to use it =)
@java_newbie you are absolutely right, maybe you can get your team to use a transpiler and some polyfills - worth the headache!

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.