I need to give array of strings inside string.
my array will be ["2", "3"]
i += "{\"name\":\"" + args.Name + "\",\"values\":[\"" + value + "\"]
What I need:
"{"name":"category1",,"values":["1"]},{"name":"category2","values":["2","1"]
Actual output:
"{"name":"category1",,"values":["1"]},{"name":"category2","values":["2,1"]
The double quotes of values removing and make it as string array. How to achieve this?
Thanks in advance :)
Try to split your value by ,.
i += "{\"name\":\"" + args.Name + "\",\"values\":[\"" + value.split(',') + "\"]
// ----------------------------------------------------------^^^^^^^^^^^-------
try join() function of jQuery
var value = ["2","3"];
var output = "{\"name\":\"Test\",\"values\":[\"" + value.join('","')+ "\"]";
console.log(output);
Related
I have a string
garments[0][1]; // The 0 and 1 can be other numbers
I need to replace the data inside the second and the third box brackets.
[0] and [1]
So that it can be
garments[4][6]
Please let me know your suggestions when you get a chance, thank you.
You can try that:
var string = 'garments[' + 4 + '][' + 6 + ']'; //in your onClick function
//To increment dynamically:
var string = 'garments[' + i + '][' + j + ']'; //i and j being variables incrementing in your loops/treatments
Update to address comments:
If you want to break "garnments[0][1]" into "garnments",0 and 1 you can do the following:
var string = "garnments[0][1]";
string = string.split('['); //string = [["garnments"],["0]"],["1]"]]
string[1].replace(']','');
string[2].replace(']',''); //string = [["garnments"],["0"],["1"]]
You can then change values and rebuild your string for further use.
It is a bit brutal though. You can use RegExp as showed by #Diego
You can use String.prototype.replace()
'garments[0][1]'.replace('[0]','[4]').replace('[1]','[6]')
For any possible string with ***[m][n] format:
Function SetNewValues(testString, n, m)
{
var keyWordLengh = testString.indexOf("[");
return testString.substring(0,keyWordLengh) + "[" + n.toString() + "][" + m.toString() + "]";
}
Where:
testString is entire string to work on, like "something[342][345]"
n,m are values to be put inside brackets :)
This would be my approach.
var string = "['foobar'][2][12]";
var match =
/\[([^\]]+)\](?:\[(\d+)\])(?:\[(\d+)\])/g
.exec(string);
console.log(match);
I want to remove the few characters in a string using index.
for example:
My input is: "5,4,3,2,1"
I want to remove the 1th to 2nd index position characters(here , to 4).
The Output should be 5,3,2,1.
Is there any predefined function in jquery or javascript to done this?
You can try to use substring function like this:
var mystring = "5,4,3,2,1";
alert( mystring.substr(0, 1) + mystring.substr(3));
JSFIDDLE DEMO
I would juse use javascripts split function for that.
So if you have
var string = "5,4,3,2,1";
than you just need to do
var splitted = string.split(",");
whereas the character in the brackets is the one you want to split on. After you did that, you can just make a new string, and build it with the array elements.
So you do something like
var string2 = splitted[0] + "," + splitted[2] + "," + splitted[3] + "," splitted[4];
I have a Javascript array in the following format;
["One","Two","Three"]
I want this to be a string in the following format;
('One','Two','Three')
I tried the following; but it says TypeError: arr.replace is not a function (I guess this is because arr is an array)
arr=arr.replace("[","(");
arr=arr.replace("]",")");
How can replace the strings [ " ] with ( ' ) as described above.
You can use Array.join():
> var a = ["One","Two","Three"];
> "('" + a.join("','") + "')"
"('One','Two','Three')"
Well, you cannot use .replace on Array. You can do the following though
var arr = ["One","Two","Three"];
arr = "('" + arr.join("', '") + "')";
You can use this code to convert your array to string
var arr = ["One","Two","Three"];
var str = "('"+arr.join("','")+"')";
alert(str);
Fiddle Demo
You have to convert it to be a string, not an array instance. Try this:
arrStr = arr.toString();
arrStr=arrStr.replace("[","(");
arrStr=arrStr.replace("]",")");
EDIT I remembered incorrectly, this would not provide the appropriate results... The output would be:
One,Two,Three.
The recommended solution is posted first by #Blender, joining the array parts together, and adding the required opening and closing brackets...
If you have to solve this with using only replace(), this would get the proper result:
arrStr = arr.toString();
arrStr=arrStr.replace(",","','");
arrStr = "('" + arrStr "')";
Removing %20 in get method?
var c=new Array(a);
(eg: a={"1","2"}) window.location="my_details.html?"+ c + "_";
and in my_details.html :
var q=window.location.search;
alert("qqqqqqqqqqqqq " + q);
var arrayList = (q)? q.substring(1).split("_"):[];
var list=new Array(arrayList);
alert("dataaaaaaaaaaaa " + list + "llll " );
and in "list" its dusplaying me "1%202";
How can I remove this %20 =space value ??
Thanks
just use this:
alert("dataaaaaaaaaaaa " + decodeURIComponent(list) + "llll " );
This should decode the %20 to space
look here: http://www.w3schools.com/jsref/jsref_decodeURIComponent.asp
If there is a space in the parameter(s), then the %20 (URL Encoding) is necessary. You cannot pass a space in a GET request.
If you need to avoid this, use POST.
As far as I can see the problem is being introduced at this line:
window.location="my_details.html?"+ c + "_";
This could be written as:
window.location="my_details.html?"+ c.toString() + "_";
The default .toString() of a JavaScript Array would be to use a delimiter of ,, i.e.
var str = ["1", "2", "3"].toString(); // 1,2,3
In you example it appears that the delimiter being used is a space. This would have been changed by something changing the default behaviour of .toString() on the Array.prototype. Try using the following:
window.location="my_details.html?"+ c.join(",") + "_";
Better to use replace() method to replace %20 to space
list.replace("%20"," ");
var str;
var displayedNum;
for (i in imgURLArray){
str = "<li photonum="+i+">" + "<a>"+ (1+i) + "</a>" + "</li>";
$("ul.selection-list").append(str);
}
I need to do this within a loop, but what happens is it prints out "11" instead of "2", because it converts to string before addition.
I have the same problem if I try to do the addition outside of the string and store in a variable as well, it still converts to string instead of doing addition.
Number(1+1) still converts to string first before turning it into a number, so it comes out 11.
Use parenthesis:
var str = "foobar" + (1+i) + "other stuff";
I have the same problem if I try to do the addition outside of the string and store in a variable as well, it still converts to string instead of doing addition.
It should not. My guess is that you are doing something wrong there too.
Update: It seems you are converting i to a string somewhere in the code you did not post.
Update 2: Don't use for..in to loop over an array. Use a normal for loop if it is really an array:
for(var i = 0, l = imgURLArray.length; i < l; i++)
But if it is an objects:
for...in will always set i as a string (as it loops over the properties of the object which are not always integers) . That means you would have to convert i before you do any addition:
... + (1 + (+i)) + ...
Update 3:
You don't always have to use such an "explicit" for loop. For example, you can traverse the array in reverse order, which makes the head shorter:
for (var i = imgURLArray.length; i--; ) {
str = "<li photonum="+i+">" + "<a>"+ (1+i) + "</a>" + "</li>";
$("ul.selection-list").prepend(str);
}
Try wrapping numbers in Number()
Like:
var i = 1;
var str = "foobar" + Number(1+i) + "other stuff";
var str = "foobar" + (1+i) + "other stuff";
You could just use the parseInt method:
var str = "foobar" + (parseInt(1+i)) + "other stuff";
The reason is due to your loop:
for (i in imgURLArray){
This iterates over all the property names of imgURLArray as strings. So you will need to use Number() to convert i to an integer:
str = "<li photonum="+i+">" + "<a>"+ (1+Number(i)) + "</a>" + "</li>";