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
| // constructon d'un Array avec 51 éléments dont 3 remplis !
let arT = Array.from( new Array ( 51 ), ( item, i ) => {
if ( i % 25 === 0 ){
return i;
}
});
klog( arT ); // voir ci-dessous
klog( arT.length ); // 51
{ // @jreaux, erreur count === length !
function arCount( tbl ){
let
count = 0;
if ( tbl.length ){
for ( let ii in tbl ){
count++;
}
}
return count;
}
klog( arCount( arT ) ); // 51 !
}
{ // count correct et newAr
function arCount( tbl ){
let
count = 0,
newAr = [];
for ( const item of tbl ){
if ( item || item === 0 ){
count++;
newAr.push( item );
}
}
return [ count, newAr ];
}
klog( arCount( arT )[0] ); // 3
klog( arCount( arT )[1] ); // 0, 25, 50
}
/*
* 0,,,,,,,,,,,,,,,,,,,,,,,,,25,,,,,,,,,,,,,,,,,,,,,,,,,50
* 51
* 51
* 3
* 0,25,50
*/ |
Partager