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.
Related
This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 4 years ago.
I declared some variables:
var availableTile1 = document.getElementById("c1");
var availableTile2 = document.getElementById("c2");
var availableTile3 = document.getElementById("c3");
var availableTile4 = document.getElementById("c4");
var availableTile5 = document.getElementById("c5");
var availableTile6 = document.getElementById("c6");
As you see, only the number is different. Is there a quicker and clearer way of defining all variables at once?
Note: I heard about "destructuring assignment", but I couldn't apply it with document.getElementById.6
Thank you for your answers.
EDIT:
availableTile.style.border = "none";
availableTile.style.backgroundColor = "transparent";
This works if availableTile is an Id, but somehow not if it is a class?!
Don't use variables. Do use an array. Populate it in a loop.
var available_tiles = [];
for (var i = 1; i <=6; i++) {
available_tiles.push( document.getElementById("c" + i) );
}
You could get a similar result by changing the HTML to make every element a member of the same class. You can then get an array-like object with
var available_tiles = document.getElementsByClassName("tile");
or
var available_tiles = document.querySelectorAll(".tile");
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());
}
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 7 years ago.
I have an url like below
http://www.grcparfum.it/home.php?section=letteradelpresidente&lang=eng
in this URL language is english
lang=eng
i wanna call different JS file when lang is different
Here's a function that can get the querystring variables in JS for you:
function $_GET(q){
var $_GET = {};
if(document.location.toString().indexOf('?') !== -1){
var query = document.location
.toString()
.replace(/^.*?\?/, '')//Get the query string
.replace(/#.*$/, '')//and remove any existing hash string
.split('&');
for(var i=0, l=query.length; i<l; i++){
var aux = decodeURIComponent(query[i]).split('=');
$_GET[aux[0]] = aux[1];
}
}
return $_GET[q];
}
Does this help at all?
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.
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..