Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda