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

197
Vistas
Cómo obtener la salida { a: 123, bcde: 4, f: 5, gh: 67, }; en javascript
const input = { a: [1, 2, 3], b: { c: { d: { e: 4, }, }, }, f: 5, g: { h: [6, 7], }, }; const outPut = { a: 123, bcde: 4, f: 5, gh: 67, }; const getAllKeysOfObject = (obj, key) => { let tempKey = key; let nextKey = '' if (obj instanceof Object && Object.values(obj).length > 0) { nextKey = Object.values(obj)[0] tempKey += nextKey; getAllKeysOfObject(obj[nextKey], nextKey) } else { return {key: tempKey, value: obj[nextKey]} } } const getObject = (input) => { let tempObj = {} Object.keys(input).forEach(key => { let value = input[key] //[1,2] console.log(value) if (Array.isArray(value)) { let tempStr = '' for(let i =0;i<value.length;i++) { tempStr += value[i] } tempObj[key] = tempStr; } else if (input[key] instanceof Object && Object.values(input[key]).length > 0) { let newObj = getAllKeysOfObject(input[key], key) tempObj[newObj.key] = newobj.value } else { tempObj[key] = input[key] } }) return tempObj } console.log(getObject(input))

Cómo obtener la salida { a: 123, bcde: 4, f: 5, gh: 67, }; en javascript

El código anterior no funciona como se esperaba para una clave que funciona bien, pero el objeto anidado falló. Obteniendo el error Uncaught TypeError: No se pueden leer las propiedades de undefined (leyendo '')"

Enlace JSFiddle https://jsfiddle.net/ankitig1602/5v2gp64r/49/

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

0

Puede crear una función recursiva con el método de reduce y, si el valor actual no es un objeto, agregar al parámetro del acumulador y también realizar un seguimiento de las claves anteriores y transmitirlas.

 const input = { a: [1, 2, 3], b: { c: { d: { e: 4, }, }, }, f: 5, g: { h: [6, 7], }, }; function getOutput(data, pk = '') { return Object.entries(data).reduce((r, [k, v]) => { const key = pk + k if (typeof v === 'object' && !Array.isArray(v)) { Object.assign(r, getOutput(v, key)) } else { r[key] = Array.isArray(v) ? Number(v.join('')) : v } return r; }, {}) } console.log(getOutput(input))

about 4 years ago · Juan Pablo Isaza Denunciar

0

Puede obtener las entradas con recursividad.

Este enfoque no necesita entregar las últimas llaves.

 const getEntries = object => Object .entries(object) .flatMap(([k, v]) => v && typeof v === 'object' && !Array.isArray(v) ? getEntries(v).map(([l, r]) => [k + l, r]) : [[k, [].concat(v).join('')]] ), input = { a: [1, 2, 3], b: { c: { d: { e: 4 } } }, f: 5, g: { h: [6, 7] } }, result = Object.fromEntries(getEntries(input)); console.log(result);

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