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

170
Visualizações
JS: best way to keep track of unique key/value pairings?

I am iterating through objects of font data and want to push or add their postscript name and style as a key/value pair. The postscript name will always be the regular version of the postscript so I need to keep track of the style with it. For example, I want my data structure to look like:

[
   { "Arial-MT", "Bold" },
   { "Arial-MT", "Regular"}
]

While iterating though, if I come across another postscript_name that is Arial-MT and style of either Bold or Regular, I don't want to add it to my data object. What is the cleanest approach to this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use an object containing Sets of strings.

const styles = {
    "Ariel-MT": new Set(["Bold", "Regular"])
};

postscript_data.forEach(([postscript_name, postscript_style]) => {
    if (styles[postscript_name]) {
        styles[postscript_name].add(postscript_style);
    } else {
        style[postscript_name] = new Set([postscript_style])
    }
});
about 4 years ago · Juan Pablo Isaza Relatório

0

I've correct the data structure you provide as an array of objects:

const data = [
   { "Arial-MT": "Bold" },
   { "Arial-MT": "Regular"},
   { "Arial-MT": "Bold" },
   { "Arial-MT": "Regular"},
];
let visited = [];
let result = [];
for(const obj of data){
  const [k,v] = Object.entries(obj)[0];
  if(!visited.includes(`${k}_${v}`)){
      const new_obj = {}
      new_obj[k] = v;
      result.push(new_obj);
    visited.push(`${k}_${v}`);
  }
}
console.log(result)

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