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

89
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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