String and state variable concatenation - reactjs

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}`;

Related

unexpected string concatenation of literals react.js

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 ..

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');

AngularJS Comma separated assignment using q.defer object

Angular Django Registration Auth - djangoAuth.js
In lines 25 through 29, a comma separated assignment takes place. When you separate each line by semi-colons instead of commas, the code no longer works. Why is that?
and when you do that have you tried to put var before defining each variable? maybe it could be that. It seems to be a multiple declarative statement, then in order to make it work with semicolons you have to put it this way :
var deferred = $q.defer(),
var url = this.API_URL + args.url;
var method = args.method || "GET";
params = params;
var data = args.data || {};

Parse url string object into host name in AngularJS

I have a scope variable that returns me an absolute url stored by the user. While displaying I would like to only show the host name for the given object.
for example.
$scope.url ="www.myurl.com/zyas?nxs"
i want to only display it as www.myurl.com.
How can I achieve this?
Have a look at the below code
string s = "www.myurl.com/zyas?nxs";
string newstr = s.split("/"); //newstr == "www.myurl.com"
You can do like this:
$scope.url ="www.myurl.com/zyas?nxs"
$scope.host = $scope.url.split('/')[0];
var hostName = $scope.url.split('/')[0];
I went with the normal split provided by JavaScript. Here's how I parsed it to the hostname and pushed it back to my JSON object.
$scope.blogposts = result.data
// console.log(urls);
$scope.blogposts.forEach(function(blog){
console.log(blog);
var a = blog.url.split('/')
// blog.push({'viewUrl':'a[0]'})
blog["viewUrl"] =a[0]
})
In a better way, you can do this without splitting and doing things
var url = new URL("http://www.myurl.com/zyas?dsfadf");
console.log(url.host); // gives the host name www.myurl.com

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