-1

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>
Kyle Underhill
  • 89
  • 15
  • 43

1 Answers1

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
user3655510
  • 81
  • 1
  • 3
  • 10