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

241
Vistas
use an alternate for flatmap in javascript

Need alternate way for flatmap in javascript lower es5 version for the below mapping.

const b = [{
  "errorname": [{
    "name": "Error 01",
    "desc_1": "Test: 01",
    "desc_2": "Testing"
  }, {
    "name": "Error 03",
    "desc_1": "Test: 03",
    "desc_2": "Testing"
  }],
}, {
  "errorname": [{
    "name": "Error 02",
    "desc_1": "Test: 02",
    "desc_2": "Testing"
  }, {
    "name": "Error 09",
    "desc_1": "Test: 09",
    "desc_2": "Testing"
  } ]
}];

var errorMap = new Map(b
  .flatMap(o => o.errorname)
  .map(({
    name,
    ...e
  }) => [name, e]));



console.log(errorMap)
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

trying for es5 with a similar kind of operation. The application is accessed using electron app which doesnot support es9

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

0

You can download a polyfill or implement your own. It should be like this

Array.prototype.flatMap = function(mapper) {
  var result = [];

  for (var i = 0; i < this.length; ++i) {
    var item = mapper(this[i], i, this);

    if (!Array.isArray(item)) {
      item = [item];
    }

    for (var j = 0; j < item.length; ++j) {
      result.push(item[j]);
    }
  }

  return result;
}

Then you will be able to call myArray.flatMap as you would do normally.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do an inner-outer reduce by nesting the calls. This will work for an ECMAScript version under 5/6.

const b = [{
  "errorname": [
    { "name": "Error 01", "desc_1": "Test: 01", "desc_2": "Testing" },
    { "name": "Error 03", "desc_1": "Test: 03", "desc_2": "Testing" }],
}, {
  "errorname": [
    { "name": "Error 02", "desc_1": "Test: 02", "desc_2": "Testing" },
    { "name": "Error 09", "desc_1": "Test: 09", "desc_2": "Testing" }
  ]
}];

var errorMap = b.reduce(function(outer, group) {
  return group.errorname.reduce(function(inner, item) {
    return inner.set(item.name, {
      desc_1: item.desc_1,
      desc_2: item.desc_2
    });
  }, outer);
}, new Map);

console.log(Object.fromEntries([...errorMap]));
.as-console-wrapper { max-height: 100% !important; top: 0; }

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