Convert each value in the array to string [duplicate] - javascript

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); });

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);

JavaScript / jQuery: how to split string with multiple values [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method

parse string and store elements to an array [duplicate]

This question already has answers here:
How to use split?
(4 answers)
Closed 9 years ago.
I have a string likes below
a_b_c_d
I hope to decode it to an array t as
t[0]->a
t[1]->b
t[2]->c
t[3]->d
Just wonder if there ia function of javascript can parse the string directly.
var string = "a_b_c_d";
var t = string.split('_');
DEMO FIDDLE
Just split the string
var t = "a_b_c_d".split("_");

How can I convert my string to a number in Javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert String variable to int in javascript?
I have the following:
var dataElapsed = $("#stats-list").attr("data-elapsed");
This creates a string called dataElapsed but I expect a number. How can I convert this?
Is there some way to convert with JQuery? or do I have to use some Javascript way?
Try this(assuming the data attributes have valid numeric values):
var dataElapsed = parseFloat($("#stats-list").attr("data-elapsed")); //for decimal.
or
var dataElapsed = parseInt($("#stats-list").attr("data-elapsed"), 10); //for integer.
You can use the .data() method to get values from data attributes
var dataElapsed = $("#stats-list").data("elapsed");

Categories

Resources