9

I have the following array of objects for example some authors and I want to map through them and return a string which has been concatenated with some formatting. I am for some reason having an issue with this fairly easy thing.

const authors = [ { id: 1, name: 'Steven'}, {id: 2, name: 'Nick'}]
let names = authors.map( (a, i) => {
  return `${a.name} is cool`
})
console.log(names)
// ["Steven is cool","Nick is cool"]
// but I really want the string "Steven is cool Nick is cool"

How can I instead get this to map through and format it to a string?

e.g. "Steven is cool Nick is cool"

2
  • Your brwoser , and its version ? Commented Jul 21, 2016 at 6:15
  • Just names.join(" ")? Commented Jul 21, 2016 at 6:45

3 Answers 3

30

Use Array#Join :

authors.map((a) => `${a.name} is cool`).join(' ');

DEMO


NOTE : join is not related to ES6 , it is old .

Sign up to request clarification or add additional context in comments.

4 Comments

@steveniseki : i update answer, i make code more elegant (brief)
"i make code more elegant (brief)" (a) => can be a => for that matter
Yep @abetteroliver, it matters because it does not comply with eslint rules; Check out aribnb JS coding conventions.
I am confused. If you want your code to comply then why don't you change it?
5

i for one prefer the use of reduce

ES5 version

autors.reduce(function (str, person) { 
  return (str+' '+person.name+ ' is cool');
}, ''); 

ES6 version

autors.reduce((str, person) => `${str} ${person.name} is cool`, '');

3 Comments

Why would you use reduce rather than join for this?
thanks another good option, more of a functional approach and works well
@demux it's not so much reduce() rather than join(), but reduce() rather than map().join(). map().join() has to loop through all of the elements in the array twice, whereas reduce() only does a single loop. So, reduce() performs better (2x). This gain is, practically speaking, meaningless unless your array is VERY big, OR if you are doing it VERY (VERY) often. But, I think it's worth thinking about these things even on a small scale so that, when it matters, you notice it.
1

Here is another version so you don't have to map --> join.. you can just reduce.

const authors = [ { id: 1, name: 'Steven'}, {id: 2, name: 'Nick'}]

console.log(authors.reduce( (p,c) => `${p} ${c.name} is cool `, ""))

4 Comments

map->join is the most performance . Check : jsfiddle.net/abdennour/2btx0c28/4
I believe reduce will always more performant because it doesn't have to create a intermediary array. Also, in your fiddle, in chrome - reduce is more performant. But I tend to fluctuate between a reduce and a map/join solution. Depends on how I am feeling.
This will leave a space at the beginning.
@AbdennourTOUMI If you want to test performance, then use a dedicated framework. Simple time measuring is pointless.

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.