This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 1 year ago.
i have the following code in my html page and as you can see i am trying to replace the occurrence of "state" with "item" and "number" with "count" in the string s
here is an example of the response text : [{"number":177,"state":"ABONDONNE"},{"number":132,"state":"ENCOURS"},{"number":6,"state":"GAGNE"},{"number":195,"state":"PERDU"},{"number":2,"state":"REPORTE"}]
here is my javascript code:
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
var s = xhttp.responseText;
s.replaceAll("state","item");
s.replaceAll("number","count");
var jsonArray = JSON.parse(s);
console.log(s);
}
xhttp.open("GET","http://localhost:8080/plot");
xhttp.send();
my problem is that after this code i get the same response text in the console:
[{"number":177,"state":"ABONDONNE"},{"number":132,"state":"ENCOURS"},{"number":6,"state":"GAGNE"},{"number":195,"state":"PERDU"},{"number":2,"state":"REPORTE"}]
what i am missing?
You need to assign a new string returned by replaceAll function.
let updatedString = s.replaceAll("state","item")
You don't assign functions's result anywhere. Change to:
s = s.replaceAll("state","item");
s = s.replaceAll("number","count");
Related
This question already has answers here:
JavaScript equivalent to printf/String.Format
(59 answers)
Closed 1 year ago.
I have a string and array
var s1 = "hello %s, i am %d years old";
var s2 =[John,24];
Expected result:
s3 = hello John i am 24 years old
I want to save the output into another string.
I'm able to display output in console
console.log(s1, ...s2)
But not able to store in other string.
I tried many things like:
var s3 = s1.format(...s2)
Any suggestions?
Unfortunately there is no string formatter available in JS, you'd have to write that manually like
let i = 0;
s3 = s1.replace(/%(s|d)/g, (_, type) => s2[i++]);
You could use the template string from ES6:
let anotherString = `Hello ${s2[0]}, I am ${s2[1]} years old`;
You could use this too:
String.prototype.format = function() {
a = this;
for (k in arguments) {
a = a.replace("{" + k + "}", arguments[k])
}
return a
}
Usage:
let anotherString = '{0} {1}!'.format('Hello', 'Word');
Than those solutions I dont see any other way to do that.
This question already has answers here:
How to use split?
(4 answers)
Closed 7 years ago.
I have a string like this 132+456 or 132-456 or 132*456..etc ,it changes dynamically but I need to split this into 132 and 456 how to do it using pure java script?
It should be as easy as:
var parts = '132+456'.split('+');
parts[0]; //132
parts[1]; //456
Like so
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
// document.writeln only for example
document.writeln(a);
document.writeln(b);
Java
String[] values='132+456'.split('+');
String firstPart=value[0]; // has 132
for js
var data = '132+456'.split('+');
var a = data[0];
var b = data[1];
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
I have write following code to split the URL
var href = document.URL;
var GlobalBoomGuid = href.split("=");
var optionid = GlobalBoomGuid[1];
If i have URL like this : www.mydomain.com?optionid=655 It's giving me "655" .
But in current scenario my URL is www.mydomain.com?optionid=655#&ui-state=dialog and it's returning me 655#&ui-state
Now i want only id . How should i do that ?
Please if you dont like the question don't mark as a Negative
Thanx in Advance :)
This gets your result
var href = document.URL;
var foo = href.split("=")[1];
var GlobalBoomGuid = foo.split("#")[0];
var optionid = GlobalBoomGuid;
fiddle
This question already has answers here:
Get a random item from a JavaScript array [duplicate]
(13 answers)
Closed 8 years ago.
Trying to add randomness to which image is displayed. Issue with saying calling and or statement.
var image = url3 | url2 | url1;
buildImage(image);
function buildImage(imagesrc) {
var img = document.createElement('img');
img.src = imagesrc;
document.getElementById('content').appendChild(img);
var images = [url3, url2, url1];
var image = images[(Math.random() * images.length)|0];
The "or" operators are not probabilistic "or" that will return one value or another. They are very specific: || will return the first value unless it's falsy, in which case it returns the second; and | converts both arguments to integers and applies the same operation to each individual binary digit.
Instead of using separate variables, make a urls array:
var urls = [
"http://example.com/img1.jpg",
"http://example.com/img2.jpg",
"http://example.com/img3.jpg"
];
You can then select a random URL by doing:
var url = urls[Math.floor(Math.random() * urls.length)];
And call buildImage:
buildImage(url);
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
My URL looks like this one:
http://www.something.hu/#/ask?kerdesPost=fdasdas%20ad%20asd%20ad%20asdas
I would like to get only fdasdas%20ad%20asd%20ad%20asdas or only kerdesPost=fdasdas%20ad%20asd%20ad%20asdas.
How can I do it via JavaScript? What is the shortest way?
You can try the following:
if((window.location.href).indexOf('?') != -1) {
var queryString = (window.location.href).substr((window.location.href).indexOf('?') + 1);
// "queryString" will now contain kerdesPost=fdasdas%20ad%20asd%20ad%20asdas
var value = (queryString.split('='))[1];
// "value" will now contain fdasdas%20ad%20asd%20ad%20asdas
value = decodeURIComponent(value);
// "value" will now contain fdasdas ad asd ad asdas (unescaped value)
}
JSFiddle
This should get you what you need.