For simple array members like that, you can use JSON.parse.
var array = JSON.parse("[" + string + "]");
This gives you an Array of numbers.
[0, 1]
If you use .split(), you'll end up with an Array of strings.
["0", "1"]
Just be aware that JSON.parse will limit you to the supported data types. If you need values like undefined or functions, you'd need to use eval(), or a JavaScript parser.
If you want to use .split(), but you also want an Array of Numbers, you could use Array.prototype.map, though you'd need to shim it for IE8 and lower or just write a traditional loop.
var array = string.split(",").map(Number);
Answer from I Hate Lazy on Stack OverflowFor simple array members like that, you can use JSON.parse.
var array = JSON.parse("[" + string + "]");
This gives you an Array of numbers.
[0, 1]
If you use .split(), you'll end up with an Array of strings.
["0", "1"]
Just be aware that JSON.parse will limit you to the supported data types. If you need values like undefined or functions, you'd need to use eval(), or a JavaScript parser.
If you want to use .split(), but you also want an Array of Numbers, you could use Array.prototype.map, though you'd need to shim it for IE8 and lower or just write a traditional loop.
var array = string.split(",").map(Number);
Split it on the , character;
var string = "0,1";
var array = string.split(",");
alert(array[0]);
angular - Converting a string into an array in Typescript? - Stack Overflow
How to convert typescript types of strings to array of strings?
typescript - convert string to a string Array in javascript - Stack Overflow
How to convert a string to an array with JavaScript/TypeScript - Stack Overflow
Types do not exist in emitted code - you can't go from a type to an array.
But you could go the other way around, in some situations. If the array is not dynamic (or its values can be completely determined by the type-checker at the time it's initialized), you can declare the array as const (so that the array's type is ["aaa", "bbb", "ccc"] rather than string[]), and then create a type from it by mapping its values from arr[number]:
const arr = ["aaa", "bbb", "ccc"] as const;
type myCustomType = typeof arr[number];
Here's an example on the playground.
For example:
const themeMode = ['light', 'dark'] as const;
type MainTheme = typeof themeMode[number];
const values = [...themeMode];
// iterate on themeMode as const assertion
values.map((theme) => console.log(theme));
For get the output you desire you could do this:
const b = 'name'
const output = JSON.stringify([b])
I get roleIds from backend, if a user has only 1 role the roleIds become string and not array so we have to convert it to array by [ ] in angular TokenService class:
get roleIds(): string[] {
let roles: string[] = JSON.parse(JSON.stringify(this.decodedToken['RoleIds']));
let roleIds: string[] = [];
if (typeof roles == "string") { // if user has "only 1 role" the roles is a string
roleIds = [roles]; // convert to array
} else { // // if user has "more than 1 role" roles is string[] array (array typeof is object)
roleIds = roles;
}
return roleIds;
}
EDIT : Use also positive lookahead to keep the 1. 2. 3.
There might be better regex ( I am very bad at regex ) but this do the trick for this precise exemple, the solution all depend on the regex you apply. but the method to use is split.
var text = "1. Lorem Ipsum. 2. Lorem Ipsum. 3. Lorem Ipsum.";
var regex = new RegExp("(?=[0-9].)");
text.split(regex);
=> OUTPUT
Array(3) [ "1. Lorem Ipsum. ", "2. Lorem Ipsum. ", "3. Lorem Ipsum." ]
Try this
console.log("1. Lorem Ipsum. 2. Lorem Ipsum. 3. Lorem Ipsum.".match(/\d+\D+/g))
Run code snippetEdit code snippet Hide Results Copy to answer Expand