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

103
Visualizações
How to use .map function to generate array from a schema

I have been working on a way to generate a array from a schema. I am almost there I am getting the strings pushed to the array but on when it iterates over the next field it initialises the array again and I am unsure of how to fix it.

I have included a sandbox that has the issue. https://codesandbox.io/s/rough-architecture-i94ph?file=/src/App.js

the output I want should be [contractorName, agencyName]

currently it is outputting log1 [contractorName] log2 [agencyName] log3 []

Any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

try this

export const generateWatchedFields = (schema) => {
  const watchingArray = [];

  function iterates(schema){

  
    Object.values(schema).map((propertySchema) => {
      if (propertySchema.properties) {
        iterates(propertySchema.properties);
      } else if (propertySchema.dependencies) {
        const push = propertySchema.dependencies.watching;
        watchingArray.push(push);
      } else {
        console.log(watchingArray);
      }
    });
    console.log(watchingArray);
  }

  iterates(schema)

  return watchingArray;
};

I'm not entirely sure if this is what you're looking for but it returns ["contractorName", "agencyName"]

about 4 years ago · Juan Pablo Isaza Relatório

0

Looks like the problem is, that you declare your watchingArray in generateWatchedFields everytime you call generateWatchedFields.

export const generateWatchedFields = (schema) => {
  const watchingArray = []; // New array on every call

  Object.values(schema).map((propertySchema) => {
    if (propertySchema.properties) {
      return generateWatchedFields(propertySchema.properties);
    } else if (propertySchema.dependencies) {
      const push = propertySchema.dependencies.watching;
      return watchingArray.push(push);
    } else {
      console.log(watchingArray);
    }
  });
  console.log(watchingArray);
  return watchingArray;
};

So you just need to declare it outside of this context, like this:

var watchingArray = [];

export const generateWatchedFields = (schema) => {    
  Object.values(schema).map((propertySchema) => {
    if (propertySchema.properties) {
      return generateWatchedFields(propertySchema.properties);
    } else if (propertySchema.dependencies) {
      const push = propertySchema.dependencies.watching;
      return watchingArray.push(push);
    } else {
      console.log(watchingArray);
    }
  });
  console.log(watchingArray);
  return watchingArray;
};
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