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

114
Vistas
how to build an object [key, value] use string got form page

I want get two element from page and put into one object

const entryElements = await page.$$('div.kpi-entry');
const contents = await Promise.all(
entryElements.map(async (element) => {
  const objValue= await (await element.$('span.text-xl'))?.innerText();
  const objKey= await (await element.$('eui-base-v0-tooltip'))?.innerText();
  const kpiObject = {objValue,objKey,};
  return obj;
})

);

obj returns

  { objValue: '1', objKey: 'A' },
  { objValue: '2', objKey: 'B' },
  { objValue: '3', objKey: 'C' },
  { objValue: '4', objKey: 'D' },
  { objValue: '5', objKey: 'E' }

but I expected

{
  A: '1',
  B: '2',
  C: '3',
  D: '4',
  E: '5',
}

what is the best wat to do this

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

0

I only adapted your own code, you should check MDN Working with Objects for more details.

const entryElements = await page.$$('div.kpi-entry');
const newObject = {};
const contents = await Promise.all(
entryElements.forEach(async (element) => {
  const objValue= await (await element.$('span.text-xl'))?.innerText();
  const objKey= await (await element.$('eui-base-v0-tooltip'))?.innerText();
  newObject[objKey] = objValue;
})

console.log(newObject)
about 4 years ago · Juan Pablo Isaza Denunciar

0

let obj = [
  { objValue: "1", objKey: "A" },
  { objValue: "2", objKey: "B" },
  { objValue: "3", objKey: "C" },
  { objValue: "4", objKey: "E" },
  { objValue: "5", objKey: "F" },
];

let map = obj.map(element => {
  return { [element.objKey]: element.objValue };
});
console.log(Object.assign(...map));

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