Home:ALL Converter>Typescript - Remove multiple char at the begin and end of a string/Get attributs of list JSON element

Typescript - Remove multiple char at the begin and end of a string/Get attributs of list JSON element

Ask Time:2018-06-08T21:57:56         Author:Helene

Json Formatter

I have this following string :

"{"element":"634634"}"

I'd like to remove {"element":" which is always the same and the last char "} the only thing which is dynamic (size can change) is 634634 how can I do that ?

Or maybe someone can help with that :

I have this following array :

Array of element I would like to make an Array of string (myArray: string[]) which contains "734734", "utruytuyt" , ... Is that possible ?

Thanks

Author:Helene,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762299/typescript-remove-multiple-char-at-the-begin-and-end-of-a-string-get-attributs
Fenton :

As your data is in JSON format, you can simply parse it.\n\n\r\n\r\nconst str = '{\"element\":\"634634\"}';\r\n\r\nconst obj = JSON.parse(str);\r\n\r\nconsole.log(obj.element);",
2018-06-08T13:59:32
Rob H :

You can also iterate over the array using Array.map() to achieve an array of those strings:\n\n\r\n\r\nconst listElement = [\r\n '{\"element\":\"734734\"}',\r\n '{\"element\":\"utruytuyt\"}',\r\n '{\"element\":\"07078\"}',\r\n '{\"element\":\"khgkhg45\"}',\r\n];\r\n\r\nconst result = listElement.map((str: string) => JSON.parse(str).element);\r\n\r\nconsole.log(result);",
2018-06-08T14:32:48
yy