Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Map and Join from Object

I tried to map and join from the object below.

production_countries: 

0: 
iso_3166_1: "GB"
name: "United Kingdom"

1: 
iso_3166_1: "US"
name: "United States of America"

I tried production_countries?.map((name) => (name.join(','))) and it doesn't render.

I want the result to be United Kingdom, United States of America

like image 248
Akhil Avatar asked Jul 23 '26 19:07

Akhil


1 Answers

I didn't try it but I think this will work.

production_countries?.map((item) => item.name).join(', ');

Because map will return array of only names then we join all elements of that array.

Edit - Trimming the last space character

With above code you may end up having a space at the end of the string. You may call trim() string method to get rid of that.

production_countries?.map((item) => item.name).join(', ').trim();
like image 151
Muhammad Hassan Javed Avatar answered Jul 26 '26 11:07

Muhammad Hassan Javed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!