dynamic array elements through loop - javascript

I am trying to create an array of images in javascript for my website when I declare array size it is working but when I am trying to take it as a dynamic array it is not showing images. can anyone tell me what's wrong with this code?
var theImages = new Array()
for (i = 0; i < theImages.length; i++)
{
theImages[i] = i+".jpg"
}

The initial size of theImages is 0.
You need to use
for (i = 0; i < 5; i++)
{
theImages[i] = i+".jpg";
}
replace 5 by the number of images you have.

This is happening because you are looping an empty array, the length/size of the array is 0 because you have just created a new array and are looping it without any elements in it yet.
If you wish to add all images to an array, you will have to know the total number of images you have/ count of images, and then run the loop to add the images in the array, which you were able to do successfully you said.
var theImages = new Array();
for (i = 0; i < 6; i++)
{
theImages[i] = i+".jpg";
}
If you are getting the image names for SQL, it is a different query you want to use, let me know if that is what you are looking for.

Related

map box js; hide and show label

I've never worked with JavaScript before. I'm trying to create a map where different layers can be hidden per users preference. I came across the helpful example on map box showing the exact code at https://www.mapbox.com/mapbox-gl-js/example/toggle-layers/.
Now, my problem is, because I have numerous point layers of different magnitudes, I was forced to create several layers and filter them based on the desired attribute so each points appearance reflects that specific magnitude. However, I want all these points to be organized into years, so I grouped the layers. So all my layer names look something like this (2006_mag2,2006_mag8,2010_mag3...).
However, I want the hide/show option to show layers based on years. So I was thinking I could do some sort of operator like we use in sql (i.e. '2006%' or a LIKE operator). Looking at some posts a lot of people use '*' in JavaScript?
So this is what it would look like for each layer individually before:
var toggleableLayerIds = [ '2006_mag2', '2010_mag3' ];
for (var i = 0; i < toggleableLayerIds.length; i++) {
var id = toggleableLayerIds[i];
}
and this is my botched attempt at trying to group a number of the layers together:
var toggleableLayerIds = [ '2006.*', '2008.*' ];
for (var i = 0; i < toggleableLayerIds.length; i++) {
var id = toggleableLayerIds[i];
}
Any guidance you guys can provide will be greatly appreciated.
You could try to use loops with regex to group layerIds by date. Sorry I changed the name of your variables. This will get you an object with layerIds grouped by date.
var layerIds = [ '2006_mag2', '2010_mag3', '2006_mag8', '2006_mag1', '2008_mag2'];
var dates = ['2006', '2010'];
var groupedLayers = {};
//init your object to have a member for each date
//this could be done inside the loops below by testing if the array exists before pushing the matching layerId
for (var i=0; i < dates.length; i++) {
groupedLayers[dates[i]] = [];
}
//loop over your layerIds to match with the RegExp
for (var i=0; i < layerIds .length; i++) {
for (var j=0; j < dates.length; j++) {
var searchPattern = new RegExp('^' + dates[j]);
if (searchPattern.test(layerIds[i])) {
groupedLayers[dates[j]].push(layerIds[i]);
}
}
}
console.log(groupedLayers)
Please tell me what exact result you need so I could help you more.

Push multi-dimension array into another array

I have an multi-dimension array that i'm iterating through. Is there a way to put the contents of the array into a new multi-dimension array creating a new MDA? For example, the following code puts all indices of the original array into the new candies array if there's a match. Currently i'm doing
candies.push([product[0],product[1], etc...]);
I'm just trying to see if there's a faster/cleaner way to get that in there.
I tried:
candies.push(product);
but that didn't work. Here's the code i'm currently using
var sel = 'candy';
var candies = [];
for(var i = 1; i < products.length; i++) {
var product = products[i];
for(var j = 0; j < product.length; j++) {
if(sel==product[11]){
candies.push([product[0],product[1],product[2],product[3],product[4],product[5],product[6],product[7],product[8],product[9],product[10],product[11],product[12]]);
}
break;
}
}
A cleaner way of writing below line:
candies.push([product[0],product[1],product[2],product[3],product[4],product[5],product[6],product[7],product[8],product[9],product[10],product[11],product[12]]);
is:
candies.push(product.slice(0,13)]);
Sample example JSFiddle is here.
Best of luck.
(Mark this as answer if it serves your need.)

Appending Items from one array to each and every element of another array in Javascript

Note: I am not asking how to append data to an array!
Rather my problem is that I want to append items to each and every element of an array.
Here is a part of my code:
dataset=[];
var xpoints=["Jan","Feb","Mar","Apr","May"];
var ypoints=[10,20,30,40,50];
for (var i = 0; i < xpoints.length; i++) {
dataset.push({
x : xpoints[i],
y : parseFloat(ypoints[i])
});
}
The array so far would be as below:
dataset[0] - {x:Jan,y:10}
dataset[1] - {x:Feb,y:20}
dataset[2] - {x:Mar,y:30}
dataset[3] - {x:Apr,y:40}
dataset[4] - {x:May,y:50}
So far there is no problem...
But if now i have another array (Suppose that it is of the same length), I want to append the new array's elements into my existing array such that my output would be as follows:
var zpoints=["a","b","c","d","e"];
/*
Do something
*/
Required Output:
dataset[0] - {x:Jan,y:10,z:a}
dataset[1] - {x:Feb,y:20,z:b}
dataset[2] - {x:Mar,y:30,z:c}
dataset[3] - {x:Apr,y:40,z:d}
dataset[4] - {x:May,y:50,z:e}
If I do:
for (var i = 0; i < dataset.length; i++) {
dataset.push({
z:zpoints[i]
});
}
it would append it as different elements in the dataset array, which is not what I am looking for.
Is the required output achieveable using JavaScript? How?
What if I want to add multiple objects to the dataset array but I do not know the number of objects to be added while compiling?
Suppose that there can be multiples arrays:
z1=["a","b","c","d","e"];
z2=["l","m","n","o","p"];
z3=...
.
.
and so on.. and the number is unknown until runtime.
I want to do something like this:(invalid code)
for(var j=0;j<length;j++) //Length will be known only during runtime
for (var i = 0; i < dataset.length; i++) {
dataset[i].z[j] = zpoints[i]; //z[j] is invalid!!
}
I need to name the objects dynamically somehow. Is there a way to achieve this?
It's rather simple:
for (var i = 0; i < dataset.length; i++) {
dataset[i].z = zpoints[i];
}
A .push call will always append more entries to the array; in this case you want to modify the existing ones.
You need to simply add new property z to existing object:
var l = zpoints.length;
while(l --)
dataset[l].z = zpoints[l];

Accessing and updating an object variable,or associative array

This is my script, which I manged to put together using code on this link :
Setting up a variable length two-dimensional array
stringWithSeper = "Panther^Pink,Green,Yellow|Dog^Hot,Top";
var blocks = stringWithSeper.split("|");
for (var i = 0; i < blocks.length; i++) {
temp = blocks[i].split("^");
result[temp[0]] = temp[1].split(",");
}
I'm confused. What's the structure of "result" ? Is it
result = {"Panther":{"Pink","Green","Yellow"}, "Dog":{"Hot","Top"}}
If not, what is it? I've been breaking my head over this for the past 3 days.
Here is your answer: alert(JSON.stringify(result))
Result: {"Panther":["Pink","Green","Yellow"],"Dog":["Hot","Top"]}

For loop information on Multi-Dimensional Array

How to add For loop information to Multi-Dimensional Array?
http://jsfiddle.net/MZj3L/
If I am trying this code get - map undefined. But how to save data something like to this ->
[[Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10]]
Thanks and sorry for my English language.
It seems in you want to initialize a multi dimensional array. Arrays are dynamic in JavaScript, you don't have to initialize them with a certain length. You could just do:
var map = [];
for(var a = 0; a < 10; a++){
map[a] = [];
}
This gives you an array containing 10 arrays.
Why are you getting undefined?
Because your syntax is way of. What map = [a][b]; does is creating an array with one element a and then accessing the bth element of that array and assign it to map.
So in the last iteration, it does:
map = [9][9];
which is the same as
tmp = [9];
map = tmp[9];
try something like
var map = [];
for(var a = 0; a < 10; a++){
map[a]=[];
for(var b = 0; b < 10; b++) {
map[a].push(b);
}
}
I am not sure what you want to do either but that's the only think I could do with your code ...

Categories

Resources