i have an image array like this
var bigImagesList = document.getElementsByClassName('monique-image');
Now i am trying to check if an image in an array is having current path like tis
for (var i = 0; i < bigImagesList.length; i++) {
if (bigImagesList[i].getAttribute('src') === currentBigImageFilePath) {
// Current Image Big Grab //
currentBigImageToDisplay = bigImagesList[i];
var currentIndex = bigImagesList[i].index;
console.log(currentIndex);
but somehow it says undefined . Please tell me how can i grab the index of the current item in if condition. thanks
When you are looping through the array, already you have the index there, as you are looping through the array of bigImagesList. So you might just need to replace:
var currentIndex = bigImagesList[i].index;
With:
var currentIndex = i;
You don't even need to create a new variable, IMO.
Related
good evening, I am trying to use a single value to search an array, and return the full line the said value is in.
The Array is set up like this in string form:
Xanax,Brand,Anxiety,Code
However, now I'm stuck with calling back only the Medication, and not the full line the Medication is in, sadly. I would like to be able to grab each variable in a line, and make them their own independent variables outside of the array so I can use them for something else.
this.importDataObject("MEDDIAGNOSISICD-10.txt", "C:/Users/dell/Documents/tab
excel/MEDDIAGNOSISICD-10.txt");
var oFile = this.getDataObjectContents("MEDDIAGNOSISICD-10.txt");
var cFile = util.stringFromStream(oFile, "utf-8");
var fileArray = cFile.split('\t');
var Med = this.getField("Medications 1");
var Index = fileArray.indexOf(Med.value);
var Call = fileArray[Index];
console.println(Call);
Any help would be wonderful!
It's because you are running the indexOf method on the whole array, you need to run it on the each value instead. Try a for loop before you check IndexOf method.
Like this:
var i, Index;
for (i = 0; i < fileArray.length; i++) {
Index = fileArray[i].indexOf(Med.value);
if(Index > -1) console.log('Your search is found in ' + fileArray[i] );
}
Note that, in here the variable Index will be 0 or larger if that search is successful. And will be of value -1 if no match is found.
I know that this is fundamental JS, but I'd like a simple explanation. From what I've read, If i declare an empty variable outside of my loop, the variable inside the loop should be accessible globally? Or am I totally wrong?
I would like to access randAd from outside my for loop.
var mobileAds = [
"mobile/bb.jpg",
"mobile/eyeko.jpg",
"mobile/farfetch.jpg",
"mobile/fsb.jpg"
];
var randNum = (Math.floor(Math.random() * mobileAds.length));
var randAd;
var i;
for (i = 0; i < mobileAds.length; ++i) {
randAd = (mobileAds[randNum]);
}
If you want to access every element of randAd outside the for loop try like this var randAd = []; to initialize it as an array. You can easily access it after your for loop but If you use it as a simple variable var randAd;then you'll get the last variable always (it overwrites). So initialize it as an array and push every element inside loop before outputting it.
var mobileAds = [
"mobile/bb.jpg",
"mobile/eyeko.jpg",
"mobile/farfetch.jpg",
"mobile/fsb.jpg"
];
var randNum = (Math.floor(Math.random() * mobileAds.length));
var randAd = []; // see the change here
var i;
for (i = 0; i < mobileAds.length; ++i) {
randAd.push(mobileAds[randNum]); // push every element here
}
console.log(randAd);
You are overthinking. You have done the hard bit in getting a random number between 0 and array's length. So, just get the ad at that index:
var randAd = mobileAds[randNum];
No need to use for loop at all.
If you would like to use randAd it should be initialised as an empty array [] and then push in that array from inside your loop randAd.push(). Like this:
var randAd=[];
var i;
for (i = 0; i < mobileAds.length; ++i) {
randAd.push(mobileAds[randNum]);
}
I have an object and an array of categories that should be kept in the object. This snip https://jsfiddle.net/h10rkb6s/2/ ( see log ) works but I cant seems to shake the idea that it is to complicated for a simple search and keep task.
var thz_icon_source = {"Spinners":["spinnericon1","spinnericon2"],"Awesome":["awesomeicon1","awesomeicon2"],"Others":["othericon1","othericon2"]};
var $categories = '["Spinners","Awesome"]';
var $CatsArray = JSON.parse($categories);
var groups = [];
for(var k in thz_icon_source) groups.push(k);
$.each($CatsArray,function(i,keep){
var index = groups.indexOf(keep);
if (index !== -1) {
groups.splice(index, 1);
}
});
for (var i = 0; i < groups.length; i++) {
delete thz_icon_source[groups[i]];
}
I tried with
$.each(thz_icon_source,function(category,icons){
$.each($CatsArray,function(i,keep){
var index = category.indexOf(keep);
if (index !== -1) {
delete thz_icon_source[category];
}
});
});
but this works only if 1 item is inside my search array.
Any help is appreciated.
There's no need to iterate over $CatsArray to find out which ones should be deleted. You will need to iterate over the keys of the object, and find out for each of them whether it should be deleted, to filter by that.
Leaving the top 3 lines of your script intact, you could simplify to
var keysToDelete = Object.keys(thz_icon_source).filter(function(groupName) {
return $CatsArray.indexOf(groupName) == -1;
});
($.grep would be the jQuery-ism for the filter method, if you are into that).
But assuming we don't even need those groups in an array, you could simply do
for (var groupName in thz_icon_source)
if ($CatsArray.indexOf(groupName) == -1)
delete thz_icon_source[groupName];
However, instead of deleting items from that object, I'd recommend to create a new object with only those that you want to keep. It's much easier to use:
var kept = {};
for (var i=0; i<$CatsArray.length; i++)
kept[$CatsArray[i]] = thz_icon_source[$CatsArray[i]];
I'm using for() to loop a function. In this function, you need to have different variable to specific which container will be update.
When loop, the variable will use string+count integer to have different var name. Example: t=1 > var title1, t=2 > var title2 etc.
Example code:-
for(t = 1; t <= 5; t++) {
var title(t) = function(e){}
}
If I use var var title+t = function(e){}, its not working.
Hope any one can help me on this.
Arrays let you store multiple values and refer to them by an index number. For example:
var title=[];
for (var t=0; t<=4; t++) {
title[t] = //something
}
You can then use an index like title[2] to access items in the array. Arrays in Javascript start counting at 0, so the first item is title[0].
I've been trying at this for hours now and I thought it would be really simple;
Using javascript I basically want to iterate through an array, get the current value of the index and then unset this value from the array. I've found splice() is supposed to work for this however I don't seem to be able to empty the array, there is always one value left on the arrary
var filtered = array("up", "down", "left");
function resetTags(){
var length = filtered.length;
for(i=0; i <= length; i++){
filtered.splice(i,1);
}
}
EDIT::
I'll try to explain in a bit more detail:
I'm basically trying to keep track of a listed of selected class values which are obtained
from when an item is clicked:
var filtered = array();
jQuery("li a").click(function () {
tag = jQuery(this).text();
addFiltered(tag);
});
function addFiltered(param){
var inArray = jQuery.inArray(param,filtered);
if(inArray > -1){
//param is in array, so we want to remove it from the filtered array
filtered.splice(index, 1);
});
}else{
//param isn't in array, so we want to add it to the array
filtered.splice(0, 0, param);
});
}
}
If you want to empty the array, set it to be an empty array directly:
filtered = [];
If you want to use the values before emptying the array, simply iterate before that without removing values and clear it when you are done.
What do you stand to gain by messing with convoluted solutions?
the array was defined incorrectly. That is why the code didn't excute
var filtered = ["up", "down", "left"];
function resetTags(){
var length = filtered.length;
for(i=0; i <= length; i++){
filtered.splice(i,1);
}
}
To remove items one by one:
var a = [1,2,3,4,5];
while (a.length > 0 ) {
a.splice(0,1);
}
http://jsfiddle.net/89hkH/
Well, you're incrementing. Have you tried decrementing?
var filtered = new Array("up", "down", "left");
function resetTags(){
var length = filtered.length;
for(i = length; i >= 0; i--){
filtered.splice(i,1);
}
}
This should make sure the final element is spliced.
I basically want to iterate through an array, get the current value
of the index and then unset this value from the array.
for(i=0; i <= length; i++){
filtered.splice(i,1);
}
I don't think you are clearly defining (or perhaps don't know), what you're trying to do.
Are you trying to write a pop(n) method such that:
var a = [1,2,3,4]
var result = pop(3, a)
result == [ 1, 2, 4]
Or are you just trying to walk an array and get the first element off every time? If so, you're doing it wrong. That's just a shift()
var filtered = ["up", "down", "left"]
for(i = 0 ; i<= filtered.length; i++)
{
alert(filtered);
filtered.shift();
alert(filtered);
}