Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda