I want to convert an array to a sting so that visually it is just the same but with quote marks around it.
For example:
array= [1,2,3,[4,5,6],[7,8,9]]
goes to => "[1,2,3,[4,5,6],[7,8,9]]"
I tried String(array) but this just punches through all the brackets to give me:
"1,2,3,4,5,6,7,8,9"
Anyone got any ideas on this?
Thanks
You could use JSON.stringify
JSON.stringify(array);
The JSON.stringify() method converts a JavaScript 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.
Use JSON.stringify
array= [1,2,3,[4,5,6],[7,8,9]]
console.log(JSON.stringify(array));
Use JSON.stringify
console.log(JSON.stringify([1,2,3,[4,5,6],[7,8,9]]));
Try
array = array.join(",");
array = array.split(",");
array = array.join("");
This should take care of the sub-arrays.
Related
I have a string value as abc:language-letters-alphs/EnglishData:7844val: . I want to extract the part language-letters-alphs/EnglishData, the value between first : and second :. Is there a way to do it without storing each substrings on different vars? I want to do it the ES6 way.
You can do this two ways easily. You can choose what suits you best.
Using String#split
Use split method to get your desired text.
The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method's call.
let str = 'abc:language-letters-alphs/EnglishData:7844val:'.split(':')
console.log(str[1]) //language-letters-alphs/EnglishData
Using String#slice
You can use [ Method but in that you have define the exact indexes of the words you want to extract.
The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.
let str = 'abc:language-letters-alphs/EnglishData:7844val:'
console.log(str.slice(4, 38)) //language-letters-alphs/EnglishData
const str = "abc:language-letters-alphs/EnglishData:7844val:"
const relevantPart = str.split(':')[1]
console.log("abc:language-letters-alphs/EnglishData:7844val:".split(":")[1])
I got following string which I'd like to be an array:
"["Arr1","Arr2"]"
I need to remove first and last chars in order to have
["Arr1","Arr2"].
I was trying with slice function like that:
var result = arr.slice(1, -1) but I got ""Arr1","Arr2"".
Any ideas how to handle it?
To convert from string to that array use JSON.parse(json_string), this does not have anything to do with removing characters from string.
I am doing some JavaScript coding and have to process a string to a array.
The original string is this: "red,yellow,blue,green,grey"
What I want to get is an array like this: ["red","yellow","blue","green","grey"]
I have tried to write a function to do this, use indexOf() to get the position of commas then do some further processing. But I think it's to heavy this way. Is there a better way to use regular expression or some existed JavaScript method to implement my purpose?
Thanks all.
use string.split function to split the original string by comma.......
string.split(",")
You can use split:
The split() method splits a String object into an array of strings by separating the string into substrings.
var arr = "red,yellow,blue,green,grey".split(',');
OR
You can also use regex:
var arr = "red,yellow,blue,green,grey".match(/\w+/g);
Try the string.split() method. For further details refer to:
http://www.w3schools.com/jsref/jsref_split.asp
var str = "red,yellow,blue,green,grey";
var res = str.split(",");
you can use .split() function.
Example
.split() : Split a string into an array of substrings:
var str = "red,yellow,blue,green,grey";
var res = str.split(",");
alert(res);
You can use following regular expression ..
[^,]*
I have such string test1/test2/test3/test4/test5
How can I get those tests in separate variables or in array or smth using javascript or jquery ?
var arrayOfBits = string.split(separator)
Use split
MN Documentation for split
var data = "test1/test2/test3/test4/test5".split("/");
You could use split (so no jQuery required) -
var arr = "test1/test2/test3/test4/test5".split("/");
console.log(arr);
Demo http://jsfiddle.net/ipr101/hXLE7/
You can use String.split(), where you specify the separator as "/" in the API, and get the array of values in return.
You can split a string by a delimiter.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split
Is there a way to separate array.toString() with semicolons instead of commas?
Check out join(). It takes an argument for the separator.
alert(myArray.join(';'));
Try using the "join" method on the array - array.join(";")
array.toString().replace(/,/g,';');
array.join(';');
var arrayAsString = array.toString();
var whatYouWant = arrayAsString.replace(/,/g, ';');
Though default separator for the join() method is comma(','), you can use other separators also. Refer this JavaScript Array Object : join() Method tutorial.