stack13
04-02-2005, 11:49 PM
Hi all.Im trying some maps here, made with
multidimensional arrays. Once declared I wanted
to modify them by removing elements first, but I found some
troubles:
map=new Array();
map[1]=new Array();
map[1][1]=new Array();
map[1][1][16]=null;
map[2]=new Array();
map[2][1]=new Array();
map[2][1][17]=null;
map[2][4]=new Array();
map[2][4][12]=null;
map[3]=new Array();
map[3][2]=new Array();
map[3][2][16]=null;
map[3][2][18]=null;
map[3][2][20]=null;
map[3][2][22]=null;
map[3][4]=new Array();
map[3][4][5]=null;
map[3][4][10]=null;
Because my map has three layers I decided to use three
dimensions [value is not used for this propose].
removing elements:
function showArray(text,object) {
document.write('<strong>'+text+'</strong>: ');
document.write('<BR>');
var c=0;
for(var i in object){
if(typeof(object[i]=="object")){
for(var e in object[i]){
if(typeof(object[i][e]=="object")){
for (var a in object[i][e]){
document.write(i+' => '+e+' => '+a+' ');
document.write('<BR>');
c++;
}
}
else {
document.write(i+':'+e+' ');
document.write(' => ');
}
}
}
else {
document.write(i+' ');
document.write(' => ');
}
}
document.write(c+' items<br>---<br>');
}
function remMapElement(array,e){
if(typeof(array[e])=="undefined") return false;
// splits because splice behaviors is different for last dimension arrays
// map[x][x]
if(array[e]!=null){
array.splice(e,1,false);
}
// map[x][x][x]
else{
array.splice(e,1);
array.unshift(0,false);
}
}
showArray('Unsorted',map);
//remMapElement(map,3)
//remMapElement(map[3],4)
remMapElement(map[3][4],5)
showArray('Sorted',map);
As you see, array.splice(e,1,false) works good for 1st,2nd dimension, but I cant find the right method for removing 3rd dimension correctly.
array.splice(e,1) will work for last indexes like remMapElement(map[3][4],10). For non-last indexes like remMapElement(map[3][4],5) it would return 3 => 4 => 9 . Is there any method I could use in this case for removing non-last indexes without shifting array's indexes? Thanks in advance.
--mac
multidimensional arrays. Once declared I wanted
to modify them by removing elements first, but I found some
troubles:
map=new Array();
map[1]=new Array();
map[1][1]=new Array();
map[1][1][16]=null;
map[2]=new Array();
map[2][1]=new Array();
map[2][1][17]=null;
map[2][4]=new Array();
map[2][4][12]=null;
map[3]=new Array();
map[3][2]=new Array();
map[3][2][16]=null;
map[3][2][18]=null;
map[3][2][20]=null;
map[3][2][22]=null;
map[3][4]=new Array();
map[3][4][5]=null;
map[3][4][10]=null;
Because my map has three layers I decided to use three
dimensions [value is not used for this propose].
removing elements:
function showArray(text,object) {
document.write('<strong>'+text+'</strong>: ');
document.write('<BR>');
var c=0;
for(var i in object){
if(typeof(object[i]=="object")){
for(var e in object[i]){
if(typeof(object[i][e]=="object")){
for (var a in object[i][e]){
document.write(i+' => '+e+' => '+a+' ');
document.write('<BR>');
c++;
}
}
else {
document.write(i+':'+e+' ');
document.write(' => ');
}
}
}
else {
document.write(i+' ');
document.write(' => ');
}
}
document.write(c+' items<br>---<br>');
}
function remMapElement(array,e){
if(typeof(array[e])=="undefined") return false;
// splits because splice behaviors is different for last dimension arrays
// map[x][x]
if(array[e]!=null){
array.splice(e,1,false);
}
// map[x][x][x]
else{
array.splice(e,1);
array.unshift(0,false);
}
}
showArray('Unsorted',map);
//remMapElement(map,3)
//remMapElement(map[3],4)
remMapElement(map[3][4],5)
showArray('Sorted',map);
As you see, array.splice(e,1,false) works good for 1st,2nd dimension, but I cant find the right method for removing 3rd dimension correctly.
array.splice(e,1) will work for last indexes like remMapElement(map[3][4],10). For non-last indexes like remMapElement(map[3][4],5) it would return 3 => 4 => 9 . Is there any method I could use in this case for removing non-last indexes without shifting array's indexes? Thanks in advance.
--mac