Calling a nested function with a string (window[]) in JS [duplicate] - javascript

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 months ago.
How to call a nested function with only a string? eg:
function oot(wha) {
function inn(wha)
{
First.innerHTML+="Inner ["+wha+"]";
}
oot.inn = inn;
Second.innerHTML+="Outer ["+wha+"]";
}
oot("1");
oot.inn("2"); //works okay
window["oot"]("3"); //works okay
window["oot.inn"]("4"); //<The problem, doesn't work.
window["oot"]["inn"]("4"); //Works, thanks.
Edited to make the code more readable, and show a solution.
IF there is no way to make this work with a single string i can probably make do, but i will leave the question unanswered for a few hours to see if there is another solution.

You can reference nested Objects like this:
window["oot"]["inn"]("4");
or
window.oot.inn("4")

Related

why do you have to add the [I]? [duplicate]

This question already has answers here:
How to get value at a specific index of array In JavaScript?
(8 answers)
Closed 2 years ago.
so I am currently learning javascript on Codecademy. And a weird kind of thing got introduced that I don't quite get.
if you look at the code. you see at the end in console that after logging animals it is a [i] like why is that there? I get that is has something to do with the for loop. But I don't quite understand like why or what that it does. I don't know if question is clear enough but if you just try to explain why it is there and what it does there. that would be greatly appreciated:)
const animals = ['Grizzly Bear', 'Sloth', 'Sea Lion'];
for (let i = 0; i < animals.length; i++){
console.log(animals[i]);
}
the bracket notation is used to specify the index of the array you are looping through. You can always read more about javascript arrays on w3schools

Google script change a value in my array unexpectedly [duplicate]

This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 5 years ago.
I am stuck with a part of my google script code where one of array elements changed unexpectedly. It looks like a bug to me, but I'd like to hear from here if it's really is or just my coding error that I overlooked. Of course, I'm hoping for a solution as well in any case.
Here is that part of the code.
if (chkIn) {arr[1] = importData[i][1]+'2';
} else {
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
arr[1] = importData[i][1]+'1';
Logger.log('---------------------------------------------------');
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
};
(The if statement doesn't seem relevant here, but I included it just in case.)
Here is the output.
2573 2017122103
2574 20171221041
2575 20171221042
20171221042
---------------------------------------------------
2573 2017122103
2574 20171221041
2575 20171221031
20171221031
I really have no idea how importData[i+2][1] changed its value to arr[1] (the number after 2575).
Thank you in advance.
Probably this is because in your case:
arr === importData[i+2]
So when you change arr[1] you also have changed importData[i+2][1].

Ajax get function array element [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 7 years ago.
Hello stackoverflow community, I need help with ajax $.get function. When I recieve array from getlist.php and i alert ir like this alert(data_list); everything works correctly. But when I try to alert like this alert(data_list.id) it doesn't work. Here is how my array looks like in console:
[{"id":"2","name":"Something","type":"horizontal","clicks":"0","start_date":"01/20/2016","end_date"
:"02/19/2016","status":"1","target":"http://","image_url":"http://","pre_exp_email":"0"},{"id"
:"1","name":"None","type":"horizontal","clicks":"2","start_date":"01/20/2016","end_date":"05/19/2016"
,"status":"1","target":"http://wps.us.lt","image_url":"http://....../wp-content/uploads/2016
/01/250by250ad.jpg","pre_exp_email":"0","group_id":["1"],"slots":{"1":"1"}}]
And here is myfunction which calls get function.
function get_list() {
jQuery.get("/wp-content/plugins/wp125/functions/getlist.php", { grouptype:jQuery('#grouptype').val() }, function(data_list){
var str;
alert(data_list.name);
});
}
If data is only a object like
var k={"id":123;"name":"abc"}
you can access id using k.id
but when it is object of Array like
var k=[{"id":123,"name":"abc"},{"id":13,"name":"ab"}];
in that case you have iterate over array something like this
for(i=0;i<k.length;i++)
{console.log(k[i].id)}

Array.indexOf Not working [duplicate]

This question already has answers here:
Why Array.indexOf doesn't find identical looking objects
(8 answers)
Closed 7 years ago.
I have an angularJS/Typescript application where I am trying to check if an object is already in a current list of objects
if (this.selectedFormatData.indexOf(item) === -1) {
//doesn't exist so add
this.selectedFormatData.push(item);
} else {
this.selectedFormatData.splice(this.selectedFormatData.indexOf(item), 1);
}
I have used this code before and it worked but isn't in this instance. Console output suggests it should work?
Any ideas?
Update: yeah correct looks like a duplicate sorry. I had a previous bit of code where i thought it worked because it was returning 0 instead of -1. Not sure why it would return 0 though
As the comments state, indexOf() is not meant to compare objects. This has been answered before here: Why Array.indexOf doesn't find identical looking objects.

searching values in array javascript [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 9 years ago.
Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:
var arr=[1,3,4,'+','-', or whatever]
function value_check(user_click){
var operators=['+','-','/','*','.']
for (var i=0;i<operators.length;i++){
if (arr[arr.length-1]==operators[i]){var value1='operator found';}
if (user_click==operators[i]){
var value2= value1;alert("consecutive operators"); break;
}
}
}
I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:
if (arr[arr.length-1] && user_click BOTH ARE IN operators array)
alert("consecutive operators)
Yes, there are some options:
JavaScript indexOf()
jQuery.inArray()
arrayName.indexOf() is what you are looking for.

Categories

Resources