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

149
Visualizações
Find JSON values return as strings

I am trying to read the JSON data to find how many instances each trait_type's value appears for each nth edition and send the value to the HTML table as a string with the trait_type value's ID. The filter function currently returns 0 for each value.

var data = [
  {
    edition: 1,
    attributes: [
      {
        trait_type: "hair",
        value: "mullet"
      },
      {
        trait_type: "hat",
        value: "cap"
      }
    ]
  }
];
var mullet = data.filter(function (d) {
    return d.value === "mullet";
  }).length,
  cap = data.filter(function (d) {
    return d.value === "cap";
  }).length;

document.getElementById("mullet").innerText = mullet;
document.getElementById("cap").innerText = cap;
<dl>
  <dt>Hair?</dt>
  <dd id="mullet"></dd>
  <dt>Hat?</dt>
  <dd id="cap"></dd>
</dl>

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

0

You're going to have to filter it deeper, currently you are just looking at the top layer of data so only the attributes key exists and the value key doesn't exist on { edition: 1, attributes: []} It only exists on the attributes array inside your data So what you need to do is to go one layer deeper, in your filter function. Like so:

function filterDataFor(type) {
 const result= data.filter(
({ attributes })=> attributes
    .filter(({value})=> value ===  type)
)
  return result.length
}
  let mullet = filterDataFor('mullet')
  let cap = filterDataFor('cap')

document.getElementById("mullet").innerText = mullet;
document.getElementById("cap").innerText = cap
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