var leaguetable = new Array();
leaguetable[0]= #leaguetable;
leaguetable[1]= #leaguetable1;
leaguetable[2]= #leaguetable2;
leaguetable[3]= #leaguetable3;
leaguetable[4]= #leaguetable4;
leaguetable[5]= #leaguetable5;
leaguetable[6]= #leaguetable6;
leaguetable[7]= #leaguetable7;
leaguetable[8]= #leaguetable8;
Can you have ID as the array values like I have done? Because this is not working for me at the moment.
You need to wrap your values inside quotes '#leaguetable1', '#leaguetable2'.....
However, you can just use a simple for loop to achieve it automatically instead of manually adding it:
var leaguetable = new Array();
leaguetable[0]= '#leaguetable';
for(var i=1; i<=8; i++) {
leaguetable[i] = '#leaguetable' + i;
}
console.log(leaguetable);
Fiddle Demo
Bo, #foo is not a valid JavaScript expression. You meant '#foo' instead.
Also, you can (should) use an array literal instead of new Array:
var leaguetable = [
'#leaguetable',
'#leaguetable7',
...
'#leaguetable8'
];
Related
I just want set 2 arrays which should contain for loop output.
Here's code:
var key_ls = new Array();
var value_ls = new Array();
for (var a in window.localStorage) {
key_ls[a] = a;
value_ls[a] = localStorage[a];
}
and it gives me no result. What do i wrong?
What you're looking for is key_ls.push(a), which will simply add another item to the array. Also use [] instead of new Array().
I want to create an array using a for loop in JavaScript. I want my array to be consisted of 10 variables or more (var kaunt1, var kaunt2, etc...) which will be actually numbers from div tags.
I tried this code below, but it isn't working??? Am I missing something?
var arr = [];
for(var i=1; i<=10; i++) {
var kaunt[i] = parseInt(document.getElementById("A"+i).innerHTML, 10);
}
var kaunt[i] = ... isn't how you add an index to an array, that's a syntax error.
Just use kaunt[i] = ....
You're declaring arr, but using kaunt? Not sure what that's about, but you should normalize that if they're meant to be the same thing.
Anyway, use kaunt.push(parseInt(document.getElementById("A"+i).innerHTML, 10)); (no var) inside your for.
Other's beat me I think, but this should do it...
var kaunt = new Array();
for(var i=1; i<=2; i++) {
kaunt[i] = parseInt(document.getElementById("A"+i).innerHTML, 10);
}
Get rid of the var in front of kaunt[i].
kaunt[i] = ....
This question already has answers here:
is the + operator less performant than StringBuffer.append()
(13 answers)
Closed 9 years ago.
I have two jQuery variables. Each variable is a text string containing words separated by a comma.
var myFirstVariable = green,blue
var mySecondVariable = circle,triangle
I would like to have a third variable retured like this:
var myThirdVariable = greencircle,bluecircle,greentriangle,bluetriangle
The order of the words in myThirdVariable is not important. But my first two variables can contain any number of words, so if
var myFirstVariable = green,blue,yellow
var mySecondVariable = circle,triangle,square
Then I need my third variable to returned like this:
var myThirdVariable = greencircle,bluecircle,yellowcircle,greentriangle,bluetriangle,yellowtriangle,greensquare,bluesquare,yellowsquare
I think I need to push() both variables into an array but I'm struggling with this area of jQuery. Hope someone can shed some light on this. Many thanks.
I'm struggling with this area of jQuery
That's simply because the jQuery library has no tools for this kind of work.
Use the native JavaScript functionality instead, specifically the String split method, the Array join method, the string concatenation operator + and for-loops:
var myFirstVariable = "green,blue"
var mySecondVariable = "circle,triangle";
var firstArr = myFirstVariable.split(","),
secondArr = mySecondVariable.split(","),
thirdArr = [];
for (var i=0; i<firstArr.length; i++)
for (var j=0; j<secondArr.length; j++)
thirdArr.push(firstArr[i]+secondArr[j]);
var myThirdVariable = thirdArr.join(",");
You can use the plain old string split method to get 2 arrays.
http://www.w3schools.com/jsref/jsref_split.asp
You could then use nested for loops to push the new strings into your 3rd array and then use the join method to create the final string.
http://www.w3schools.com/jsref/jsref_join.asp
Try
var myFirstVariable = 'green,blue'
var mySecondVariable = 'circle,triangle'
var myThirdVariable = fn(myFirstVariable, mySecondVariable);
console.log(myThirdVariable)
function fn(fv, sv){
var fa = fv.split(','), sa = sv.split(','), ta = [];
for(var i = 0; i < fa.length; i++){
for(var j = 0; j < sa.length; j++){
ta.push(fa[i] + sa[j])
}
}
return ta.join(',')
}
Demo: Fiddle
OK so you don't need jquery to achieve this, just JavaScript.
check out this answer here to help you:
How to merge two arrays in Javascript and de-duplicate items
I have multiple variables containing JSON as string (received from AJAX).
data.output_data_1234
data.output_data_5678
I convert them to Array:
var outputdataarr = new Array(data.output_data_1234);
This works fine, but how do I add a number to the var name:
var outputdataarr = new Array('data.output_data_'+formid+'');
this one does not work.
formid contains a proper number.
This does not work too:
var outputvar = window['data.output_data_' + formid];
var outputdataarr = new Array(outputvar);
Please help. Thanks.
You probably mean, you need something like this:
var outputdataarr = new Array(data['output_data_'+formid]);
You can only use string in square brackets as an object field identifier. It cannot contain '.'.
UPDATE:
However, you will probably need a loop to fill the whole array, e.g.
var outputdataarr = new Array();
for (var i=1000; i<2000; i++) {
outputdataarr.push(data['output_data_'+formid]);
}
Use [] instead of new Array is better.
var outputdataarr = [];
outputdataarr.push(data['output_data_'+formid]);
//and so on
I am a newbie in JS. Here is my code and I believe it should work... but it doesn't.
var pop = new Array();
pop['la'] = new Array('nt','gb','te');
pop['sa'] = new Array('nt','gb');
pop['ha'] = new Array('pc','pa');
var _ecpop="la";
for (var i = 0; i < pop[_ecpop].length; i++)
{
document.write(pop[_ecpop][i]);
}
I just do not know any alternate way to have a map of vectors of a string.
Thanks,
Amir.
That's not an Array, but a Javascript Object, containing Arrays in it's properties. You can use Object and Array literals for that. The advantage is that your code looks much cleaner. There are seldom reasons to use new Array or new Object in javascript code (see for example this SO Question).
var pop = {
la: ['nt','gb','te'],
sa: ['nt','gb'],
ha: ['pc','pa']
}
now you can use
for (var i = 0; i < pop.la.length; i++) {
console.log(pop.la[i]);
}
if a property label is stored in a variable (like you _ecpop), you can use bracket notiation to retrieve it's value:
var laArr = pop[_ecpop];
for (var i = 0; i < laArr.length; i++) {
console.log(laArr[i]);
}
The other way around you can assign a label to an Object:
var _ecpop = 'la';
pop[_ecpop] = ['nt','gb','te'];
document.write is not the preferred way to put things on your page. It's better and just as easy to use some element with an id, and write output to it using innerHTML, for example
document.getElementById('myOutput').innerHTML = '[some output here]';
In javascript, an array can only have numeric indexes, if you want to use textual indexes, you should use object instead.
var pop = new Object();
or
var pop = {};
and then:
pop['la'] = new Array('nt','gb','te');
However, as an object is not an array, it has no length member, but just as an array you can use the for..in to go through all of its values.
Using document.write is not a good choice as it only works during the document loading, not after it. Try to use text nodes or innerhtml instead.