Convert string into several integer arrays [duplicate] - javascript

This question already has answers here:
convert string into array of integers
(12 answers)
Closed 5 years ago.
How do i break down this " 01|15|59, 1|47|6, 01|17|20, 1|32|34, 2|3|1 " string into 5 integer arrays ?.
For example:
01|15|59 becomes [01,15,59]
1|47|6 becomes [1,47,6]

var result = string.trim().split(", ").map(el=>el.split("|").map(n=>+n))
this splits the string into an array of these groups ( yet as string )
"1|2, 3|4" => ["1|2","3|4"]
then maps this array to a new array containing the strings splitted and converted to numbers:
=> [[1,2],[3,4]]

Related

Find first occurrence of a string in a String array in Typescript/Javascript [duplicate]

This question already has answers here:
How to find the array index with a value?
(12 answers)
split an array into two based on a index in javascript
(8 answers)
Closed 3 years ago.
I have an empty string array. I push into it new strings, and I do this one by one. Then, it will happen that the string I'm pushing is already in the array, and I want to find the index of that original string and split the array in the range:
[start, index_of_firstString].
So if we have:
myArray: string[] = [];
function(myString: string) {
this.myArray.push(myString);
}
What could be a good solution? I need to check for duplicates every time I push a new string.
You can use of indexOf function to get the first occurence of your string in your strings array,
var cars = ['ferrari','Audi','ferrari']
console.log(cars.indexOf('ferrari'))

Convert raw string to array [duplicate]

This question already has answers here:
How to convert an array like string to array in node.js?
(4 answers)
Closed 3 years ago.
I need to convert raw String to json array in javascript below the my logic
Original:
"[template1,template2]";
Exception:
"["template1","template2"]";
use slice to retrieve the text between the square brackets and then use split.
const input = "[template1,template2]";
const arr = input.slice(1, -1).split(',');
console.log(arr);

js list of strings to string [duplicate]

This question already has answers here:
Convert array to JSON
(12 answers)
Closed 4 years ago.
I need to convert list of strings
const ar: string[] = ['str1', 'str2']
to a string which contains ar container with square brackets [] and quotes "
const str: string = `["str1", "str2"]`
how to do it in proper way?
The format you want is a JSON array string. So use the JSON object's stringify function.
JSON.stringify(["some", "array", 123]);

what method can I use to separate strings inside array into individual arrays (using JavaScript)? [duplicate]

This question already has answers here:
Convert string with commas to array
(18 answers)
Closed 5 years ago.
var array = ["Charizard,Pikachu,Glalie,Delfox"];
I tried to use the split() method except that only affects string variables and not arrays.
My ultimate goal is to make array = ["Charizard","Pikachu","Glalie","Delfox"];
You can retrieve the value of the array an split it using as delimiter the comma ','
var array = ["Charizard,Pikachu,Glalie,Delfox"];
console.log(array[0].split(','))

Convert each value in the array to string [duplicate]

This question already has answers here:
Convert integer array to string array in JavaScript
(11 answers)
Closed 6 years ago.
I have this array
var p = [0,1,2,3,4];
and I want to convert each value in the array to string like
var p = ['0','1','2','3','4'];
any ideas, help please?
You can convert number to string simply by prepending empty string to it.
p = p.map(function(e){return ""+e});
All you need to do is pass the Number value to the String function to get a string out of a Number.
[0,1,2,3,4].map(function(value) { return String(value); });

Categories

Resources