IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

supprimer les colonnes vides d'un array Javascript


Sujet :

JavaScript

  1. #21
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2009
    Messages
    354
    Détails du profil
    Informations personnelles :
    Localisation : France, Sarthe (Pays de la Loire)

    Informations forums :
    Inscription : Février 2009
    Messages : 354
    Par défaut
    si tu veux aussi supprimer les lignes vides ...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
     
    var compact2DMatrix = function(){
        //private globals
        var isEmptyCol = function(array, indexCols){
            for(var i = 0, l = array.length, cell; i < l; i++){
                cell = array[i][indexCols];
                if(cell =! undefined && cell != null && cell != '')
                    return false;
            }
            return true;
        };
     
        //public
        return function(array){
            var res = [], 
            indexCols = 0,
            colsLength = 0;
     
            //clear empty lines
            for(var i = 0, li = array.length, col; i < li; i++){
                col = array[i];
                if(!(col instanceof Array))
                    continue;
     
                colsLength = col.length > colsLength ? col.length : colsLength;
     
                for(var j = 0, lj = col.length, cell; j < lj; j++){
                    cell = col[j]
                    if(cell =! undefined && cell != null && cell != ''){
                        res.push(col);
                        break;
                    }
                }
            }
     
            //clear empty colonnes
            for(; indexCols < colsLength; indexCols++){
                if(isEmptyCol(res, indexCols)){
                    for(var i = 0, l = res.length; i < l; i++)
                        res[i].splice(indexCols, 1);
                    colsLength--;
                    indexCols--;
                }
            }
     
            return res;
        }
    }();
     
     
    var le_tableau=new Array();
    le_tableau[0]=new Array('cell 1 1', '', 'cell 1 2','cell 1 3', '', '', 'cell 1 5', '');
    le_tableau[1]=new Array('cell 2 1', '', 'cell 2 2','cell 2 3', '', '', 'cell 2 5', '');
    le_tableau[2]=new Array('cell 3 1', '', 'cell 3 2','cell 3 3', '', '', 'cell 3 5', '');
    le_tableau[3]=new Array('', '', '', '', '', '', '', '');
    le_tableau[4]='';
    le_tableau[5]=new Array('cell 6 1', '', 'cell 6 2','cell 6 3', '', '', 'cell 6 5', '');
     
    alert(compact2DMatrix(le_tableau));

  2. #22
    Membre émérite Avatar de sebhm
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2004
    Messages
    1 090
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Landes (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Avril 2004
    Messages : 1 090
    Par défaut
    Nickel

    j'ai fait que tester, je vais maintenant tacher de comprendre.

    Par contre, sur nos premiers échanges, il était où le mal-entendu ?


  3. #23
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2009
    Messages
    354
    Détails du profil
    Informations personnelles :
    Localisation : France, Sarthe (Pays de la Loire)

    Informations forums :
    Inscription : Février 2009
    Messages : 354
    Par défaut
    j'ai un peu amélioré le dernier code supprimant les lignes et colonnes

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
     
    var compact2DMatrix = function(){
        //private globals
     
        //return if a line is empty
        var isEmptyLine = function(line){
            if(!(line instanceof Array)) 
                return true;
            for(var j = 0, lj = line.length, cell; j < lj; j++){
                cell = line[j];
                if(cell =! undefined && cell != null && cell != '')
                    return false;
            }
            return true
        },
     
        //return if a colonne is empty
        isEmptyCol = function(array, indexCols){
            for(var i = 0, l = array.length, cell; i < l; i++){
                cell = array[i][indexCols];
                if(cell =! undefined && cell != null && cell != '')
                    return false;
            }
            return true;
        };
     
        //public
        return function(array){
            //clear empty lines
            var colsLength = 0;
            for(var indexLine = 0, lineLength = array.length, line; indexLine < lineLength; indexLine++){
                line = array[indexLine];
                if(isEmptyLine(line)){
                    array.splice(indexLine, 1);
                    indexLine--;
                    lineLength--;
                }
                else colsLength = line.length > colsLength ? line.length : colsLength;
            }
     
            //clear empty colonnes
            var indexCols = 0;
            for(; indexCols < colsLength; indexCols++){
                if(isEmptyCol(array, indexCols)){
                    for(var i = 0, l = array.length; i < l; i++)
                        array[i].splice(indexCols, 1);
                    colsLength--;
                    indexCols--;
                }
            }
     
            return array;
        }
    }();
     
     
    var le_tableau=new Array();
    le_tableau[0]=new Array('cell 1 1', '', 'cell 1 2','cell 1 3', '', '', 'cell 1 5', '');
    le_tableau[1]=new Array('cell 2 1', '', 'cell 2 2','cell 2 3', '', '', 'cell 2 5', '');
    le_tableau[2]=new Array('cell 3 1', '', 'cell 3 2','cell 3 3', '', '', 'cell 3 5', '');
    le_tableau[3]=new Array('', '', '', '', '', '', '', '');
    le_tableau[4]='';
    le_tableau[5]=new Array('cell 6 1', '', 'cell 6 2','cell 6 3', '', '', 'cell 6 5', '');
     
    alert(compact2DMatrix(le_tableau));
    à tester ...

    le problème fut que tu aurais dut nous dire direct ou se trouvait les lignes et les colonnes dans ton tableau, nous on a fait au plus simple en supposant que les colonnes correspondais au index du tableau globale...

    a+

  4. #24
    Membre émérite Avatar de sebhm
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2004
    Messages
    1 090
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Landes (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Avril 2004
    Messages : 1 090
    Par défaut
    ah OK dsl.

    je croyais être clair avec mon second post

    en tout cas merci et bravo pour ton code !

  5. #25
    Membre éclairé
    Profil pro
    Inscrit en
    Février 2009
    Messages
    354
    Détails du profil
    Informations personnelles :
    Localisation : France, Sarthe (Pays de la Loire)

    Informations forums :
    Inscription : Février 2009
    Messages : 354
    Par défaut
    ah OK dsl.

    je croyais être clair avec mon second post
    autant pour moi, c'est moi qui n'a pas bien lut tout les msg bye

  6. #26
    Membre chevronné
    Profil pro
    Inscrit en
    Juin 2009
    Messages
    313
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2009
    Messages : 313
    Par défaut
    Une autre approche :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    function suppress(ary) {
       var i, j, n;
       n=ary[0].length;
       for (i=0; i<ary.length; i++) { // Empty rows ?
          j=0;
          while ( (ary[i][j] === '' || ary[i][j] == null) && (++j < n) ) {}
          if (j == n) {
             ary.splice(i,1)
             i--;            
          }
       }
     
       n=ary.length;
       for (j=0; j<ary[0].length; j++) { // Empty cols ?
          i=0;
          while ( (ary[i][j] === '' || ary[i][j] == null) && (++i < n) ) {}
          if (i == n) {
             while (--i >= 0) {ary[i].splice(j,1)}
             j--;            
          }
       }
       return ary;
    }
     
    var le_tableau=new Array();
    le_tableau[0]=new Array('cell 1 1','cell 1 2',null,'cell 1 4','');
    le_tableau[1]=new Array('cell 2 1','cell 2 2','','cell 2 4','');
    le_tableau[2]=new Array('','','','','');
    le_tableau[3]=new Array('cell 4 1','cell 4 2','','cell 4 4','cell 4 5');
    alert((suppress(le_tableau)).toSource())
    Le malentendu venait d'une confusion entre Array et tableau (Un vrai tableau) et par extension, entre index et colonne.

  7. #27
    Membre émérite Avatar de sebhm
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2004
    Messages
    1 090
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Landes (Aquitaine)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Avril 2004
    Messages : 1 090
    Par défaut
    voici la version que j'utilise :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    var compact2DMatrix = function(){
        //private globals
     
    	//return if a cell is empty
    	var isEmptyCell = function(cellule_content) {
    		//on vire les guillemets et les espaces (dont insécable)
    		if(cellule_content != undefined && cellule_content != null) {
    			cellule_content+='';	//cast to string
    			cellule_content=cellule_content.replace(/"/g,'').replace(/ /g,'').replace(/\u00a0/g,'');	//suppr des espaces (+insecable avec unicode) et des guillemets "
    		}
    		//test
    		if(cellule_content != undefined && cellule_content != null && cellule_content != '')
    			return false;
    		return true;	
    	};
     
        //return if a line is empty
        var isEmptyLine = function(line){
            if(!(line instanceof Array)) 
                return true;
            for(var j = 0, lj = line.length, cell; j < lj; j++){
                cell = line[j];
                //if(cell != undefined && cell != null && cell != '')
    			if (!isEmptyCell(cell))
                    return false;
            }
            return true
        };
     
        //return if a colonne is empty
        var isEmptyCol = function(tab, indexCols){
            for(var i = 0, l = tab.length, cell; i < l; i++){
                cell = tab[i][indexCols];
    			if (!isEmptyCell(cell))
                    return false;
            }
            return true;
        };
     
        //public
        return function(tab){
            //clear empty lines
            var colsLength = 0;
            for(var indexLine = 0, lineLength = tab.length, line; indexLine < lineLength; indexLine++){
                line = tab[indexLine];
                if(isEmptyLine(line)){
                    tab.splice(indexLine, 1);
                    indexLine--;
                    lineLength--;
                }
                else colsLength = line.length > colsLength ? line.length : colsLength;
            }
     
            //clear empty colonnes
            for(var indexCols = 0; indexCols < colsLength; indexCols++){
                if(isEmptyCol(tab, indexCols)){
                    for(var i = 0, l = tab.length; i < l; i++)
                        tab[i].splice(indexCols, 1);
                    colsLength--;
                    indexCols--;
                }
            }
     
            return tab;
        }
    }();
    j'ai ajouté une fonction qui teste si la cellule est vide.
    Dans mon cas, j'ai besoin de tester plus qu'une cellule vide : j'enleve les guillemets et les espaces (dont l'espace insécable car on vient d'HTML)

    un grand

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. [Tableaux] Comment enlever les elements vide d'un array
    Par BernardT dans le forum Langage
    Réponses: 9
    Dernier message: 07/07/2006, 13h12
  2. CListCtrl Supprimer les colonnes.
    Par Bmagic dans le forum MFC
    Réponses: 2
    Dernier message: 26/06/2006, 13h35
  3. Réponses: 2
    Dernier message: 04/05/2006, 14h10
  4. supprimer les lignes vides?
    Par VinnieMc dans le forum Langage
    Réponses: 5
    Dernier message: 27/02/2006, 15h01
  5. [excel]comment supprimer une colonne vide...
    Par Mugette dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 19/10/2005, 14h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo