Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
Nested for..of iterate over first array Element

I have Data Structure like this

const array = [
  [["first_name" , "First"], ["image" , "image"]],
  [["email" , "email"]],
  [["last_name","last_name"]],
  [["password" , "password"], ["password", "password"]]
]

My Iterator Implementation is

let arr = array.values();
let obj = {
  [Symbol.iterator]: function () {
    return {
      next() {
        for (const iterator of arr) {
          for (let [, value] of iterator) {
            return {
              done: false,
              value,
            };
          }
        }

        return {
          done: true,
        };
      },
    };
  },
}

But when I iterate it [...obj]

I am getting Only first element of 2D array

The output I am getting is ["First" , "email" , "last_name" , "password"]

Expected output ["First" , "image" , "email" , "last_name" , "password", "password"]

Nested for..in iterate only 1st element of nested array

Note:- Above solution is working fine with Symbol.Iterator generator function

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Like Nenad Vracar's answer, since it is returned from the inner for loop statement, it does not loop all the inner arrays, only the outer array loops and ends.

Individual states must be able to be saved to implement what you want with Symbol.iterator alone without using Generator.

const array = [
  [["first_name" , "First"], ["image" , "image"]],
  [["email" , "email"]],
  [["last_name","last_name"]],
  [["password" , "password"], ["password", "password"]]
];

const obj = {
  [Symbol.iterator]: () => {
    return {
      stack: [...array],
      target: [],
      next() {
        if (!this.target.length && !this.stack.length) return {
          done: true,
          value: undefined,
        }
        if (!this.target.length) this.target = this.stack.shift();

        return {
          done: false,
          value: this.target.shift()[1],
        };
      },
    }
  }
};

console.log([...obj]); // ['First', 'image', 'email', 'last_name', 'password', 'password']
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda