JSON parsing syntax impossible [duplicate] - javascript

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I need to rotate a DIV, but that is not the problem I am facing, it is JSON parsing,
I want to be able to get the proper attr value from the variable rotateAttrSet but I want to get it according to the browser type
I can do var rotateAttr = rSET.FF;, but I can't do var rotateAttr = rSET.brwShort;
Is there a way to make this work?
again, I am not looking for ways to rotate the DIV, I just want to know if there is a way to get the JSON work by a variable (like .brwShort below)
Thanks
<script>
var rotateAttrSet = '{"IE":"-ms-transform","FF":"-moz-transform","CR":"-webkit-transform","SF":"-webkit-transform","OP":"-o-transform","WC3":"transform"}';
function rotator(o)
{
var o = $(o);
var angle = 0;
var rSET = parseJSON(rotateAttrSet);
var brwShort = "FF";//getBrowser().split(";")[2];
var rotateAttr = rSET.brwShort;
//alert(rotateAttr);
//o.removeAttr("onClick");
setInterval(function(){
angle++;
if(angle == 360) angle = 0;
o.text(angle);
o.css(rotateAttr, "rotate("+angle+"deg)");
}, 10);
}
function parseJSON(s)
{
return eval('('+s+')');
}
</script>

You need to use the browser short as a key as follows:
var brwShort = "FF";//getBrowser().split(";")[2];
var rotateAttr = rSET[brwShort];
Otherwise, it is actually looking for a property on the object with a key of "brwShort", which doesn't exist on your object.

Related

How can i change the variable name in javascript? [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed last year.
I have some js code like this:
var parameter0 = 12345;
var parameter1 = 54321;
var parameter2 = 33333;
var parameter3 = 99990;
function selectValue(number) {
alert(parameter+number);
}
selectValue(2);
Here is a fiddle - https://jsfiddle.net/frwd2qLg/
This code will not work, because, for example, for number = 2, it will not show 33333, but will be undefined. Any workaround?
As people said in your comments, you could use an array or an object to do this task. But answering your question you culd use an eval to access the variable name.
var parameter0 = 12345;
var parameter1 = 54321;
var parameter2 = 33333;
var parameter3 = 99990;
function selectValue(number) {
alert(eval("parameter"+number));
}
selectValue(2);

How to get the value of a variable from a object in JavaScript? [duplicate]

This question already has answers here:
Javascript: How to use Template Literals with JSON?
(4 answers)
Closed 2 years ago.
I am having the object config and when I fetch the config["RegionId"], it will give me ${Region}. Now I want to fetch the value of Region. As I have got ${Region}, I thought I could do console.log(`${val}`) to get 'abc'. But it is not working.
How to get this? This is what I have tried,
var config = {
"RegionId" : "${Region}"
}
var Region = 'abc'
var val = config['RegionId']
console.log(`${val}`)
Don't put double quotes around the property value. Use back-ticks (for template literal strings) as you are doing for val. And make sure you've declared Region prior to attempting to access it.
var Region = 'abc';
var config = {
"RegionId" : `${Region}`
};
var val = config['RegionId'];
console.log(`${val}`);
And while your question is centered around template literals, I hope you know that for this scenario, they add no value and are overkill. Here's the same thing with regular strings variables:
var Region = 'abc';
var config = {
"RegionId" : Region
};
console.log(config['RegionId']);
try using eval function
var config = {
"RegionId" : "${Region}"
}
var Region = 'abc'
var val = config['RegionId']
console.log(eval("`"+val+"`"));

Variable in a variable name? Looping said variables? [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 5 years ago.
So I have a chunk of code... and I want to make it more efficient by condensing it to a few lines instead of twelve. The idea I had was to use the variable of a loop to call each variable in sequence, since the code is just repeated with different numbers each time. Is there any way this could work?
var usetext1 = getText("text1");
var usetext2 = getText("text2");
var usetext3 = getText("text3");
var usetext4 = getText("text4");
var usetext5 = getText("text5");
var usetext6 = getText("text6");
usetext1 = usetext1.toUpperCase();
usetext2 = usetext2.toLowerCase();
usetext3 = usetext3.toLowerCase();
usetext4 = usetext4.toLowerCase();
usetext5 = usetext5.toLowerCase();
usetext6 = usetext6.toLowerCase();
Reduced to something like:
for (var i=2;i<6;i++){
var usetext[i]=getText("text[i]");
usetext[i]=usetext[i].toLowerCase();
You can use Template Literals to store the value into an array
var arr = [];
for (var i=1; i <= 6; i++){
arr.push(getText(`text${i}`).toLowerCase());
}

How to append a string into an object 'path' in Javascript? [duplicate]

This question already has answers here:
Dynamically set property of nested object
(28 answers)
Closed 7 years ago.
I have this path in a string: var path = 'bonds.international.emergent' and the object portfolio.
I want to access portfolio.bonds.international.emergent, using the path in the path variable.
Something like portfolio[path], which obviously doesn't work. Anyone knows how to do this?
If it's only dots, you can split:
var path = 'bonds.international.emergent';
var target = portfolio;
path = path.split('.');
for(var i = 0; i < path.length; i++) {
target = target[path[i]];
}
alert(target); // = portfolio.bonds.international.emergent
Else, you can eval:
var target = eval('portfolio.' + path);
But avoid eval if you can.

How to extract a GET parameter from the URL in Javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Use the get parameter of the url in javascript
Suppose I have this url:
s = 'http://mydomain.com/?q=microsoft&p=next'
In this case, how do I extract "microsoft" from the string?
I know that in python, it would be:
new_s = s[s.find('?q=')+len('?q='):s.find('&',s.find('?q='))]
I use the parseUri library available here:
http://stevenlevithan.com/demo/parseuri/js/
It allows you to do exactly what you are asking for:
var uri = 'http://mydomain.com/?q=microsoft&p=next';
var q = uri.queryKey['q'];
// q = 'microsoft'
(function(){
var url = 'http://mydomain.com/?q=microsoft&p=next'
var s = url.search.substring(1).split('&');
if(!s.length) return;
window.GET = {};
for(var i = 0; i < s.length; i++) {
var parts = s[i].split('=');
GET[unescape(parts[0])] = unescape(parts[1]);
}
}())
Think this will work..

Categories