Navigate back to the homepage

how to convert array to string in typescript?

Infinitbility,ย 
TypeScript,ย 
July 19th, 2022 ยท 1 min read
how to convert array to string in typescript?

Convert array to string in typescript

To convert array to string in typescript, use the join() method it will convert the array to a comma (,) separated string.

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

1const arr: Array<string> = ["infinitbility","aguidehub","sortoutcode"];
2
3// โœ… Convert array to string
4const str1: string = arr.join();
5console.log(str1); // ๐Ÿ‘‰๏ธ 'infinitbility,aguidehub,sortoutcode'
6
7const str2: string = arr.join('');
8console.log(str2); // ๐Ÿ‘‰๏ธ 'infinitbilityaguidehubsortoutcode'
9
10const str3: string = arr.join('-');
11console.log(str3); // ๐Ÿ‘‰๏ธ 'infinitbility-aguidehub-sortoutcode'

Convert an array of object to string in typescript

To convert an array of objects to string in typescript, use JSON.stringify() it will convert your array of objects to string.

The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

1interface domain {
2 name: string,
3 domain: string,
4}
5
6const arr: Array<domain> = [
7 {
8 name: "infinitbility",
9 domain: "infinitbility.com"
10 },
11 {
12 name: "aguidehub",
13 domain: "aguidehub.com"
14 }
15 ];
16
17// โœ… Convert array of object to string
18const str: string = JSON.stringify(arr);
19console.log(str); // ๐Ÿ‘‰๏ธ '[{"name":"infinitbility","domain":"infinitbility.com"},{"name":"aguidehub","domain":"aguidehub.com"}]'

Note: The toString() and join() method only work for array of string, number not object.

I hope it helps you, All the best ๐Ÿ‘.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to remove space from string in typescript?

how to remove space from string in typescript?

TypeScript, add remove space from string example with step-by-step guide.

July 18th, 2022 ยท 1 min read
how to add double quotes in string in typescript?

how to add double quotes in string in typescript?

TypeScript, add double quotes in string example with step-by-step guide.

July 17th, 2022 ยท 1 min read
ยฉ 2020โ€“2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]