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

92
Visualizações
object map with the name

I have an object (obj), I need to map or loop through the dataToInsert object and display the result as shown in the data object. The complexity is that I need to display part of the name of the object in the field value (FOI_OMG_101 to 101)

const dataToInsert = {
FOI_OMG_101 : {
name : "jghj.pdf",
value: "base64"
}
}

const data=
   {
     field: "101",
     fileName: "jghj.pdf",
     value:"base64"
   }

this is what I have now

for (const [key, value] of 
Object.entries(dataToInsert.FOI_OMG_101)) {
console.log(`${key}: ${value}`);
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to split the key on those objects in the loop, and take the value from the last index of this split key as:

export default function App() {
  const dataToInsert = {
    FOI_OMG_101: {
      name: "jghj.pdf",
      value: "base64"
    },
    FOI_OMG_102: {
      name: "abc.pdf",
      value: "base64"
    }
  };

  const [arrData, setArrData] = useState([]);

  useEffect(() => {
    let newArr = [];
    Object.keys(dataToInsert).map((keyName, index) => {
      const fieldNameArr = keyName.split("_");
      newArr = [
        ...newArr,
        {
          field: fieldNameArr.at(-1),
          fileName: dataToInsert[keyName].name,
          value: dataToInsert[keyName].value
        }
      ];
    });
    setArrData(newArr);
  }, []);

  useEffect(() => {
    console.log("arrData", arrData);
  }, [arrData]);

  return (
    <div className="App">
      {arrData.map((item, index) => {
        return <li key={index}>{JSON.stringify(item)}</li>;
      })}
    </div>
  );
}

This is just an example of splitting, you can handle it as you wish. Here, is the sandbox link.

about 4 years ago · Juan Pablo Isaza Relatório

0

const dataToInsert = {
  FOI_OMG_101 : {
    name : "jghj.pdf",
    value: "base64"
  }
};
const data = {
  field: "101",
  fileName: "jghj.pdf",
  value:"base64"
};
//  Look at the entries of the full object,
//  so you do not have to know the FOI_OMG_101 field name beforehand.
for ([ field, file ] of Object.entries( dataToInsert )) {
  console.log( field );  //  FOI_OMG_101
  console.log( JSON.stringify( file ));  // {"name":"jghj.pdf","value":"base64"}
  const output = {
    field: field,
    fileName: file.name,
    value: file.value
  };
  console.log( JSON.stringify( output ));  // {"field":"FOI_OMG_101","fileName":"jghj.pdf","value":"base64"}
}

You can add additional string manipulation if you have to parse the number 101 out of the string FOI_OMG_101.

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