unexpected string concatenation of literals react.js - reactjs

to={routeName.tutorClassView + '?classId=' + `${_.get(value, '_id')}`}
to={routeName.tutorClassView + '?classId=' + `${_.get(v, 'id')}` + '&providerId=' + `${_.get(v, 'provider.id')}`}
What am i doing wrong here

You can use template string literal like this
to={`routeName.tutorClassView?classId=${_.get(value, '_id')}`}

you can entirely make use of template literals as below
to = `${routeName.tutorClassView}?classId=${_.get(value, "_id")}`;
to = `${routeName.tutorClassView}?classId=${_.get(v, "id")}&providerId=${_.get(
v,
"provider.id"
)}`;
with curly braces {} you are already in JS land so they might not be required when dealing with static and variable values combined like above ..

Related

Convert moment.Duration into string

I have a moment.Duration object which I need to convert to string format HH:mm:ss (without any timezone conversion). I know we can do like
foobar.hours + ":" + foobar.minutes + ":" + foobar.seconds
But honestly I was hoping to have a better solution that just string manipulations.
var now = moment()
var then = moment().subtract(3, 'hours').subtract(20, 'minutes')
const duration = now.subtract(then)
duration.format("HH:mm:ss")
>> 03:20:00
moment().format('HH:mm:ss');
if u have already moment object use like this
object.format('HH:mm:ss');

String and state variable concatenation

I am creating url by concatenating constant string and state variables as follows :
var url={"http://localhost:3000/get-players/"+{this.state.city}+"/"+{this.state.sport}};
But I am getting error. Can someone tell me where am I making mistake.
You're string concatenation is incorrect, you could use string interpolation like this:
var url = `http://localhost:3000/get-players/${this.state.city}/${this.state.sport}`
If you don't want to use ES6 templating, do it the old way but remove the extra brackets ({}) around the variables.
var url = "http://localhost:3000/get-players/" + this.state.city + "/" + this.state.sport;
var url={"http://localhost:3000/get-players/"+{this.state.city}+"/"+{this.state.sport}};
{this.state.city} - is not string.
1: Correct variant for ES5:
var url="http://localhost:3000/get-players/" + this.state.city + "/" + this.state.sport;
this.state.city and this.state.sport should be declared and should be a type of string.
2: Correct variant for ES6
var url=`http://localhost:3000/get-players/${this.state.city}/${this.state.sport}`;

Add Single quotes where string contains comma in AngularJS

I have a string which contains values like
var string = A,B,C
Here I want to add single quote for each comma value, expected result should be as below
output = 'A','B','C'
My angular code is
var data = {
output : this.string.split("\',"),
}
which is giving result as
["A,B,C"]
Could anyone please help on this how can I get desired output.
I am understanding your code as
var string = "A,B,C"; // because string should be in this format.
and you just need to replace "\'," from your split function to "," which will give you an array like this
var out = string.split(",");
console.log(out);
[ 'A', 'B', 'C' ] // this is the output.
as split function searches for the given expression and split the string into array.
but if you just want to modify the string without making it in array then you can use the below trick
var out = "'" + string.replace(/,/g, "','") + "'";
console.log(out);
'A','B','C' // result as u mentioned and this is of string type.

How to get string representation of an expr in Nim template

Is there a possibility to get string representation of an expression (or an identifier) inside a template? For example, having the next code:
template `*`*(name: expr) {.immediate.} =
var `name`* {.inject.}: string = ""
# Want to print 'name' here, not its value like with backticks
Is it possible to get the string representation of the name expression inside a template?
You can use the astToStr magic from the system module to do this:
template foo*(name: untyped) =
var `name`* {.inject.}: string = ""
# Want to print 'name' here, not its value like with backticks
echo "the variable name is ", name.astToStr
foo test
The output will be:
the variable name is test
The use of the immediate pragma is discouraged with the latest version of the compiler. See the following answer for more details:
typed vs untyped vs expr vs stmt in templates and macros

How can I add strings to array in javascript

I wanna create an array in javascript which looks like this:
[0,0,0,0,0,1,1,0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1,0,1,0,1,1,1,0],[0,0,0,0,1,1,1,1,0,1,0,0,1,1,0],[0,0,0,0,0,1,1,0,1,0,1,0,1,0,0]
My problem is that I don't know how to add the opening and closing square brackets to the start and the end of the output string.
here's my code:
game = new Array();
for(row=0;row<matrix.length;++row){
game[row]=matrix[row].join(',');
}
document.getElementById('jsvalue').value=game.join('],[');
document.getElementById('name2').value = name;
I tried a few things, but they didn't seem to work and all I got were errors or this output:
0,0,0,0,0,1,1,0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1,0,1,0,1,1,1,0],[0,0,0,0,1,1,1,1,0,1,0,0,1,1,0],[0,0,0,0,0,1,1,0,1,0,1,0,1,0,0
How could I add them? Is there a simple array method that I missed and would solve my problem?
Thanks in advance!
It looks like you are trying to set the value of an HTML element to the format you described in your question. However, you are not setting the value of that HTML element to an Array - you are setting it to a string. the .join function outputs a string. If indeed you want the value to be set to a string formatted in the way you described, then you could take advantage of .join, but have to do a little bit in addition to what you are doing:
game = new Array();
for(row=0;row<matrix.length;++row){
game[row]= "[" + matrix[row].join(',') + "]";
}
document.getElementById('jsvalue').value=game.join(',');
document.getElementById('name2').value = name;
If you are using join to create the string, then why not just manually add the brackets?
For example:
document.getElementById('jsvalue').value= '[' + game.join('],[') + ']';

Resources