shortcut symbol for converting toString [duplicate] - javascript

This question already has answers here:
What's the best way to convert a number to a string in JavaScript?
(25 answers)
Closed 1 year ago.
As you know, if I want to the get numeric value of a variable (e.g. $event) I'd do:
<ax-text-box (valueChange)='documentSearchItem.fromPrice=(+$event)'></ax-text-box>
I add a + to do that.
Is there a simple way like this, to get the string value of a variable ?
p.s. I don't want to use a method. I want to be able to use it in HTML templates.

let v = 2 // whatever value
let s = v.toString()

Lots of ways. A short one without methods is using template strings:
let inputNum = 222;
let newString = `${inputNum}`;
console.log(typeof(newString));

Related

Concatenate variable name with variable value Javascript [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 months ago.
I have a flexible amount of titles, aka title1, title2, title3 etc. How can i create a dynamic variable that gives me the value of that field?
function getTitle(i){
var myScope={}
myScope["title"+i]="title"+i // should i use this in any way, and how?
var output = props.attributes.title1 // This works, but it is static. Instead the 1, add the value of i here like:
var output = props.attributes.title+i
return output
}
I would like to concatenate the value of i to the word 'title'. So it becomes title1 or title2 etc.
You can make it dynamic like this
var output = props.attributes[`title${i}`];
If I understood your question right you can use template literals.
function getTitle(i) {
let title = 'myTitle'
let output = `${title} ${i}`
console.log(output)
return output
}
getTitle('concated')

Given the path of property key, how to retrieve it? [duplicate]

This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Javascript: Get deep value from object by passing path to it as string [duplicate]
(5 answers)
Closed 5 years ago.
Consider I've an object myObj
And I've a string representing the path to some property inside it: foo.bar
What is the best way to get it from my object?
If I knew the string ahead I would do myObj.foo && myObj.foo.bar to get it safely
A simple solution would be to split the string 'foo.bar'.split('.') and than loop over it.
But i'm sure there is a better way
It's a duplicate of other question. they provided a great solution:
given a path and an obj get the property value this way
path.split('.').reduce((o, i) => o[i], obj)
A simple solution would be to split the string 'foo.bar'.split('.') and than loop over it.
Yep, that sounds like the best way. You can create a helper method that does exactly this, but there's nothing built in to the language or standard libraries to make things simpler than this.
function getFromPath(obj, path) {
var current = obj;
for(let piece of path.split('.')) {
current = current[piece];
}
return current;
}
Usage:
getFromPath({foo: {bar: "hello"}}, "foo.bar"); // "hello"

JS arrays - advanced assignment [duplicate]

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 6 years ago.
This is related to the arrays in Javascript, which I am trying to use in a complex logic.
Consider the following code:
a['a1'] = 'AJA'
We know that, this is same as a.a1 = 'AJA' (provided proper definitions were given).
So, if we go ahead and interpret this:
console.log(a.a1[0])
console.log(a.a1[1])
console.log(a.a1[2])
console.log(a.a1)
It logs :
A
J
A
AJA
Now, all I need is to assign a new character at the 4th position.
When I try a[a1][3] = 'Y' or a.a1[3] = 'Y' and then try console.log(a.a1), It still displays AJA instead of AJAY.
I know that we can do this using string concatenation, i.e.
a['a1'] = a['a1'] + 'Y' and get this accomplished.
But why wasn't the first method working? By what other ways can do this?
Strings are immutable. It means that if you create a string, you can't modify it anymore. So your a1 doesn't know anything about 4th character.
You can see this example. I try to change the second char of the already created string, but it will not be changed anymore.
let a = {};
a['a1'] = 'AJA';
a.a1[1] = 'A';
console.log(a.a1);
For more you can see MDN Documentation
As I know a[a1][3] or a.a1[3] is a string variable, you can treat it as:
var s = 'ss';
When you evaluate s[0] you'll get a string value. So when you assign any string value to s, you'll not get 'ss' + anyvalue but anyvalue instead. :)

Tricking Javascript variable [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
I have a question that I could not find the answer, or perhaps cannot phrase the way it should...
I would like to trick javascript's way of handling variables...
Let's say in php I could do something like:
$test['usr_'.$id]=826
But when I try to do the same in Javascript/jQuery:
$("#usr_rank_h").val('rank_'+id);
It will output rank_826 instead of the value of the var rank_826
The equivalent idiom in javascript is actually
var id = 826;
var test = {};
test['rank_'+id] = 826;
Which gives you back an object of the form
{
'rank_826': 826
}
PS: I'm not sure why you are using jQuery in this case, are you getting the id from an input ?

Evaluate a variable to use as a variable name? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Javascript Objects: Dynamic variable names?
I have a json string passed in from a cgi script. This json string has a list of id's. In javascript
var informationObj = jQuery.parseJSON(information);
tid = informationObj.idList[0].id;
tid is now an ID and I want to use it to access objects within the json string itself like so:
alert (informationObj.tid.rpath);
However this does not seem to work. I have also tried:
alert (informationObj.eval(tid).rpath);
Is there a way around this?
Thanks.
You need this form:
informationObj[tid].rpath
They are equivalent:
var a = 'something';
b[a] === b.something
Use bracket notation:
alert(informationObj[tid].rpath);

Categories