I would like to define the following in Javascript:
var tileXml = new Array();
var tileTextAttribute = new Array();
var tileImageAttribute = new Array();
var tileNotification = new Array();
As:
var tileXml,tileTextAttribute,tileImageAttribute,tileNotification = new Array();
I should be right. For some reason it fails in the second case, why?
No you cannot do that, instead to this
var tileXml = [],tileTextAttribute = [],tileImageAttribute = [],tileNotification = [];
Related
I want to delete an array entry.
I have already used the splice method, but it doesn't work.
var clients = new Array();
//-----
var tmp = new Array();
tmp["connection"] = connection;
tmp["authentificated"] = 1;
tmp["username"] = rows[0].username;
tmp["rank"] = rows[0].rank;
clients.push(tmp);
This doesn't work:
clients.splice(index, 1);
Do you have any ideas, where the mistake is?
Thanks in advance.
you can use like below.
var clients = new Array();
var tmp = new Array();
tmp["connection"] = "hi";
tmp["authentificated"] = 1;
clients.push(tmp);
var tmp1 = new Array();
tmp1["connection"] = "hi";
tmp1["authentificated"] = 2;
clients.push(tmp1);
var index = 0;
delete clients[index];
console.log(clients);
but the issue with delete is that the length of the array will remain same because delete only removes the object from the element in the array
Is this array with in array declaration correct? and if it is correct how can I output or atleast alert all the contents in the chkArray?
var chkArray = { tv_type[],screen_size[],connectivity[],features[]};
var tv_type = [];
var screen_size = [];
var connectivity = [];
var features = [];
Define array according variable scope
var tv_type = [];
var screen_size = [];
var connectivity = [];
var features = [];
//incorrect syntax
/*var chkArray = { tv_type[],screen_size[],connectivity[],features[]};*/
//change to
var chkArray = [tv_type,screen_size,connectivity,features];
For debug Try this
alert(JSON.stringify(chkArray))
OR
console.log(chkArray)
I think you want an object with arrays in it even though you are asking for arrays in array. It's more meaningful for your usecase. A bit like this
var chkArray = {
tv_type: [],
screen_size: [],
connectivity: [],
features: []
}
To access them you can say:
chkArray.tv_type[index]
And push items:
chkArray.tv_type.push("LCD");
You can not declare a multidimensional array like this
Possible solution will be
var arrayA = [];
var arrayB = [];
var arrayC = [];
var arrayTotal = [arrayA,arrayB,arrayC];
and you can use
console.log(arrayTotal)
to print the array in console
I'm trying to create a 3 dimensional array dynamicall in javascript based on a flat array of objects. after looping through the array, the array seems empty. If I print while in the loop, it seems to work, but then it seems to be gone and I want to return this to the caller. Help ?
//init the 3d array ??
this.teams = [];
for(var i = 0; i < sportsStandings.length; i++) {
var item = sportsStandings[i];
if(!this.teams[item.league])
this.teams[item.league] = new Array();
if(!this.teams[item.league][item.division])
this.teams[item.league][item.division] = new Array();
this.teams[item.league][item.division][this.teams[item.league][item.division].length]
= new Team(item.teamName, item.wins, item.losses);
console.log(this.teams); //this prints properly, and i see the 3d array grow
}
console.log('second' + this.teams); //this prints nothing
I cleaned up the code a bit, there is a couple of other ways to write it.
this.teams = [];
var teams = this.teams;
for(var i = 0; i < sportsStandings.length; i++) {
var ss = sportsStandings[i],
league = ss.league,
division = ss.division,
teamName = ss.teamName,
wins = ss.wins,
losses = ss.losses;
if (!teams[league]) {
teams[league] = {};
teams[league][division] = [];
} else if (!teams[league][division]) {
teams[league][division] = [];
}
var newTeam = new Team(teamName, wins, losses);
teams[league][division].push(newTeam);
}
A few days ago I posted a thread asking on how to find the missing element when passing a method 2 JS arrays. As you can see here. I've been trying to figure out now how to modify the method so that instead of passing it 2 Arrays you pass it 2 2D-Arrays... Having some trouble though:
/*var sml = new Array();
sml[0] = new Array("dean","22");
sml[1] = new Array("james","31");
sml[2] = new Array("ludwig","35");
var lrg = new Array();
lrg[0] = new Array("dean","22");
lrg[1] = new Array("james","31");
lrg[2] = new Array("ludwig","35");
lrg[3] = new Array("kevin","23");
lrg[4] = new Array("elton","40");*/
var sml = new Array();
sml[0] = "dean";
sml[1] = "james";
sml[2] = "ludwig";
var lrg = new Array();
lrg[0] = "dean";
lrg[1] = "james";
lrg[2] = "ludwig";
lrg[3] = "kevin";
lrg[4] = "elton";
var deselected = findDeselectedItem(sml, lrg);
alert("Deselected Items: " + deselected[0]+", "+ deselected[1]);
// -------------------------------------------------------------- //
function findDeselectedItem(CurrentArray, PreviousArray) {
var CurrentArrSize = CurrentArray.length;
var PreviousArrSize = PreviousArray.length;
var deselectedItems = new Array();
// loop through previous array
for (var j = 0; j < PreviousArrSize; j++) {
// look for same thing in new array
if (CurrentArray.indexOf(PreviousArray[j]) == -1)
deselectedItems.push(PreviousArray[j]);
}
if (deselectedItems.length != 0) {
return deselectedItems;
} else {
return null;
}
}
Now if you run the above code it works perfectly, but if you go and uncomment the variable declarations on top that that pushes arrays ontop of the array, and then comment out the simple strings that get pushed on top of the array, it doesn't work as well... For instance:
var sml = new Array();
sml[0] = new Array("dean","22");
sml[1] = new Array("james","31");
sml[2] = new Array("ludwig","35");
var lrg = new Array();
lrg[0] = new Array("dean","22");
lrg[1] = new Array("james","31");
lrg[2] = new Array("ludwig","35");
lrg[3] = new Array("kevin","23");
lrg[4] = new Array("elton","40");
/*var sml = new Array();
sml[0] = "dean";
sml[1] = "james";
sml[2] = "ludwig";
var lrg = new Array();
lrg[0] = "dean";
lrg[1] = "james";
lrg[2] = "ludwig";
lrg[3] = "kevin";
lrg[4] = "elton";*/
var deselected = findDeselectedItem(sml, lrg);
alert("Deselected Items: " + deselected[0]+", "+ deselected[1]);
// -------------------------------------------------------------- //
function findDeselectedItem(CurrentArray, PreviousArray) {
var CurrentArrSize = CurrentArray.length;
var PreviousArrSize = PreviousArray.length;
var deselectedItems = new Array();
// loop through previous array
for (var j = 0; j < PreviousArrSize; j++) {
// look for same thing in new array
if (CurrentArray.indexOf(PreviousArray[j][0]) == -1)
deselectedItems.push(PreviousArray[j][0]);
}
if (deselectedItems.length != 0) {
return deselectedItems;
} else {
return null;
}
}
The method returns 2 completely wrong values. PS - I'm not interested in the "numbers" just yet, just the "names" for now...
Your CurrentArray is 2-dimensional and indexOf compares Arrays but not first elements of that Arrays. So you need to use:
for ( var i = 0; i < CurrentArray.length; ++i){
if (CurrentArray[i][0] == PreviousArray[j][0]){
deselectedItems.push(PreviousArray[j][0]);
break;
}
}
Instead of
if (CurrentArray.indexOf(PreviousArray[j][0]) == -1)
deselectedItems.push(PreviousArray[j][0]);
You could also rearrange the array like this:
var sml = {};
sml["dean"] = 22;
sml["james"] = 31;
sml["ludwig"] = 35;
var lrg = {};
lrg["dean"] = 22;
lrg["james"] = 31;
lrg["ludwig"] = 35;
lrg["kevin"] = 23;
lrg["elton"] = 40;
and use:
function findDeselectedItem(c,p){
ret=[];
for (var i in p){
if (p.hasOwnProperty(i)){
if ('undefined'===typeof c[i]) {
ret.push(i);
}
}
}
return ret;
}
alert(findDeselectedItem(sml, lrg));
Demo: http://jsfiddle.net/LsrCj/
indexOf function will check object equality in this case; to make it return something else than -1 you've to pass same 2D array instances to sml and lrg.
new Array("dean","22") === new Array("dean","22") //false
Keep the same instances (e.g. http://jsfiddle.net/3TQYz/) in both arrays or use your own indexOf test that would recursively check values of the array to make your case working.
I'm having some issues with the following code:
var tmpArray = new Array();
for(var n in fnlArray){
if(fnlArray[n] == largest.val){
alert(fnlArray[n] +"-"+ largest.val);
tmpArray[n] = fnlArray[n];
}
}
fnlArray contents is:
fnlArray['result1'] = 1;
fnlArray['result2'] = 2;
fnlArray['result3'] = 2;
fnlArray['result4'] = 2;
and largest.val = 2;
The issue I'm having is the alert gets fired so I would expect to end up with tmpArray with the following:
tmpArray['result2'] = 2;
tmpArray['result3'] = 2;
tmpArray['result4'] = 2;
But the array (tmpArray) is always empty. Is this an issue with adding items to the array dynamically within a loop?
var tmpArray = new Array(); should be:
var tmpArray = {};
Your tmpArray object is not a index array, so you have to use object literals.
var tmpArray = {};
for(var n in fnlArray){
if(fnlArray[n] == largest.val){
tmpArray[n] = fnlArray[n];
}
}
alert(JSON.stringify(tmpArray)); //Prints: {"result2":2,"result3":2,"result4":2}
Demo: http://jsfiddle.net/QhFGF/