The length property returns different types of results in the same loop

Viewed 38

It happens that there is an array of objects that within each object has a property with an array. This array, in the same cycle, is parsed sometimes correctly and other times incorrectly. It is observed in the image that inside the object the array has elements, however sometimes it shows length 0 and other times it shows a correct length.

The problem that I present is not only in the browser console. When the code processes the array, it actually considers it to have array 0 when it does have elements in it.

Basically what happens is that it analyzes the length of an array to see if it needs to add or remove elements, however due to asynchrony that I don't understand, there are times when in the same cycle it considers that there are elements and other data sets that also has data; it parses it as if it had no data.

I have created promises, async await, observables and nothing works to parse the array correctly.

The const cantidad_que_hay parses the length sometimes correctly and sometimes incorrectly in the same loop. It's not just a console issue, it does indeed parse it incorrectly, independent of the browser console.

 Promise.all(promesa_procesandoimagenes)
      .then( (data: any) => { 
        
          console.log('ViendoSiSuscribeSirve Promise.all.data', data)
          data.forEach( (valor:any, indice:any) => {
              of(valor).pipe( delay(0) )
              .subscribe( data2 => {
                
                console.log('ViendoSiSuscribeSirve data2', data2 )
                console.log('ViendoSiSuscribeSirve data2.item_imagen[0]', data2.item_imagen[0])
                console.log('ViendoSiSuscribeSirve data2.item_imagen[1]', data2.item_imagen[1])
                console.log('ViendoSiSuscribeSirve data2.item_imagen[1].flat(2)', data2.item_imagen[1].flat(5))
                console.log('ViendoSiSuscribeSirve data2.item_imagen[1].flat(2).length **length**', data2.item_imagen[1].flat(2).length)
                console.log(
                    'ViendoSiSuscribeSirve this.dinamicMap.default_images[item_unitario]',
                    this.dinamicMap.default_images[data2.item_imagen[0]]
                    )

                
                const cantidad_que_hay = data2.items.flat().length   // SuperAvanceDesktop: 2 
                console.log('ViendoSiSuscribeSirve data2.item_imagen[1] **length**', cantidad_que_hay)
                const array_imagenes_default = this.dinamicMap.default_images[data2.item_imagen[0]]
                const nombre_seccion = data2.item_imagen[0]
                if (cantidad_que_hay > data2.min && cantidad_que_hay < data2.max){
                  // Si está dentro del rango, return ok
                  objeto_imagenes_listo[nombre_seccion] = data2;
                }
                /*  data2.min */
                if(  (data2.min - cantidad_que_hay ) > 0  ){
                  // PONER: (Poner data2.min - data2.item_imagen[1].length)

                  const cantidad_a_poner = data2.min - cantidad_que_hay;
                  if(nombre_seccion == 'super_avance_desktop'){
                    console.warn('ViendoSiSuscribeSirve [...data2.item_imagen[1]]', [...data2.item_imagen[1]])
                    console.warn('ViendoSiSuscribeSirve', cantidad_que_hay)
                    console.warn('ViendoSiSuscribeSirve data2.item_imagen[1].flat(2)', data2.item_imagen[1])
                    console.warn('ViendoSiSuscribeSirve data2.item_imagen[1].flat(2)', data2.item_imagen[1].flat())
                  }
                  array_imagenes_default.some( (imagen:any, indice:any) => {
                    if( indice == (cantidad_a_poner - 1 ) ){
                      // Listo
                      objeto_imagenes_listo[nombre_seccion] = data2;
                    }else{
                      // Agregar
                      data2.item_imagen[1].push( {imagen: imagen} )
                    }
                  } )


                }
                
                if(  (data2.max - cantidad_que_hay) < 0  ) {
                  // SACAR : (Poner data2.max - data2.item_imagen[1].length)

                  const cantidad_a_sacar = data2.max - cantidad_que_hay;
                  data2.item_imagen[1].splice(1 , cantidad_a_sacar)
                  objeto_imagenes_listo[nombre_seccion] = data2;
                }
                
              
              }) // Fin Subscribe
        
          } )
    
      } )  // ** Fin PromiseAll

Image showing correct analysis of array length Image showing incorrect parsing of array length

0 Answers
Related