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

170
Vistas
Assign values from one set of key/value pairs to keys of another?

I'm working with the outputs of an Intranet I don't control.

I have this string:

let template = 'LAWYER=|FIRM=|SUIT_DESCRIPTION=|DEF_COMMENT=|PLF_COMMENT=|';

It goes on longer, but that's the pattern.

Now there's another similar string, but with data assigned, as in this example:

let current= 'FIRM=Smith and Wesson LLP|SUIT_DESCRIPTION=It\'s a royal mess|PLF_COMMENT=some freeform text|LAWYER=Bob Smith';

Now, notice that not every element in template is necessarily represented in current, and the order may be different (if the latter fact is a big deal, I can ensure the order is the same).

What I'm trying to do, is take every element that is in current, and populate the matching element in template, if it exists. (or, alternatively and potentially preferred, insert every non-matching element in template into current, but ideally in the same order as template).

Using the date above, the result I'm looking for is:

result = 'LAWYER=Bob Smith|FIRM=Smith and Wesson LLP|SUIT_DESCRIPTION=It\'s a royal mess|DEF_COMMENT=|PLF_COMMENT=some freeform text|';

I'm not very accomplished with JavaScript :(

I tried various things in JSFiddle using split() and match() but I just made a mess of it.

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

0

// Convert the template to an array of keys
const getKeys = str => str.split('|').map(entry => entry.split('=')[0]);

// Convert the data to an object
const toObj = str => Object.fromEntries(str.split('|').map(entry => entry.split('=')));

// Reconcile the data with the template
const compile = (templateStr, dataStr) => {
  const keys = getKeys(templateStr);
  const data = toObj(dataStr);
  return keys.reduce((results, key) => {
    if(key) results.push([key, data[key] ?? '']);
    return results;
  }, []);
};

// Convert the results back into a string
const toString = data => data.map(entry => entry.join('=')).join('|') + '|';

// And then a test
let template = 'LAWYER=|FIRM=|SUIT_DESCRIPTION=|DEF_COMMENT=|PLF_COMMENT=|';
let current = 'FIRM=Smith and Wesson LLP|SUIT_DESCRIPTION=It\'s a royal mess|PLF_COMMENT=some freeform text|LAWYER=Bob Smith';

console.log(toString(compile(template, current)));

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