This question already has answers here:
JS replace not working on string [duplicate]
(2 answers)
I want to replace '\' with '/'
(1 answer)
Closed 4 years ago.
I'm trying to replace all instances of 'formattedTime' in a massive string.
Here's my code
googleTrends.interestOverTime({keyword: selectedTopic})
.then((results) => {
results.replace(/formattedTime/g, 'no what the heck');
console.log(results);
})
.catch((err) => {
console.error('Oh no there was an error', err);
});
I am not receiving any errors however nothing is happening inside my console.
I even tried replacing a single line string but none of it will work. It might be because of the promise I'm receiving?
I am relatively new to RegEx so I thought maybe that was an issue but it doesn't seem to be.
Strig.prototype.replace does not mutate the string. Strings are immutable. It will return a new string.
Related
This question already has answers here:
Function with forEach returns undefined even with return statement
(5 answers)
Why does this forEach return undefined when using a return statement
(5 answers)
Short circuit Array.forEach like calling break
(30 answers)
Closed 8 days ago.
I'm learning the basics of javascript and I ran into this exercise which confused me a bit. The exercise is to manually create a method like "includes." So when I implement it like that:
function includes(num,arr) {
arr.forEach(val => {
if (val===num) return true;
return false;
})
}
const arrr=[1,2,3,4];
console.log(arrr.includes(2));
it works.
but when I implement it like that, which makes much more sense to me:
function includes(num,arr) {
arr.forEach(val => {
if (val===num) return true;
return false;
})
}
const arrr=[1,2,3,4];
console.log(includes(2,arrr));
, it doesn't and the output is "undefined."
Can someone explain me the logic behind it? the first way doesn't even matches the functions arguments and works, while the second way does match the arguments and doesn't.
Thanks.
This question already has answers here:
Curly Brackets in Arrow Functions
(3 answers)
Closed 2 years ago.
I spend some time figuring out what was the problem with this
arr.filter((order)=>{
order.trackingNumbers.some((track)=>{
track.number==search
})
})
and then noticed that it works if is written in just one line
arr.filter(order=>order.trackingNumbers.some(track=>track.number==search))
why is that and is posible to write it on several lines?
arr.filter((order)=>{
return order.trackingNumbers.some((track)=>{
return track.number==search
})
})
you can omit return keyword when you're omitting bracket thats why the single line works
This question already has answers here:
Curly Brackets in Arrow Functions
(3 answers)
Why doesn't my arrow function return a value?
(1 answer)
Closed 6 years ago.
I came across a little problem in my code which was kind of confusing to me and hope someone could explain why it does the things it does.
Code 1
sendText(){
return this.http.get('/api')
.map((response:Response) => response.json());
}
Code 2
sendText(){
return this.http.get('/api').map((response:Response) => {
response.json();
});
}
The key difference between these two code is that in Code 2 I placed the brackets after the arrow function to add my tasks inside those brackets and in Code 1 I took the brackets out and place the task on one line.
My question is why does my object coming from the server side coming back as undefined in Code2 with the subscribe method that angular2 provided while Code1 returns the object I suspect.
(response:Response) => response.json()
This is shorthand for this:
(response:Response) => { return response.json(); }
The {} let you add multiple statements inside the block. Without them, the function just runs the one statement and returns its value.
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to check if a string is a valid JSON string in JavaScript without using Try/Catch
The question was already asked here : How to check if a string is a valid JSON string in JavaScript without using Try/Catch. No valid answer was given, even the validated one wasn't answering the question.
So it seems the only way to not doing this using try/catches is via regexps; the regexp given in one of the answers was only validating against eval, so a simple string like "2001-05-06" would pass the regexp, even though it's not JSON.
So has anyone a good regexp to validate against a well formed JSON string ?
Using a regex instead of a try/catch is replacing a correct solution with a non working hack.
The link you give suggests to change the JSON parsing code that you can modify to not throw an exception. I would suggest replacing in json_parse.js the following code
error = function (m) {
// Call error when something is wrong.
throw {
name: 'SyntaxError',
message: m,
at: at,
text: text
};
},
by the call of a callback you would provide.
But to be frank, my most reasonable suggestion would be to use try/catch. That's the best tool you have here. Note that my JSON.parse modification "suggestion" supposes to manage the break from all loops/recursions, which is precisely what an exceptions does.
from link try this
var jsonData = '{"1":"test"}';
if (/^[\],:{}\s]*$/.test(jsonData.replace(/\\["\\\/bfnrtu]/g, '#'). replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'). replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
alert('ok');
}else{
alert('no');
}
This question already has answers here:
AJAX: Check if a string is JSON?
(8 answers)
Closed 9 years ago.
I tried that:
var c = $.parseJSON(something here)
and I control that:
c === undefined
This works however it throws error while trying to parse an invalid JSON string. I don't want it throw that error.
Any advices?
It's generally considered bad practice to suppress/ignore errors, instead why not use a try-catch block to capture the exception and do something with it:
try {
var c = $.parseJSON(something here);
}
catch (err) {
// Do something about the exception here
}
If you really don't need to do anything about the exception at least put a comment to that effect in your try-catch block, it'll make your code more readable when you come back to it later.