🏠 back to Observable

how to d3.difference on CSV?

I am sure this is something simple, but I haven’t managed to figure it out:

I have two CSV files, each with a list of values. I uploaded these CSVs as file attachments and then went about trying to use d3.difference() to identify values in one missing from the other.

…But the output isn’t the difference I was hoping for, rather, it appears to be returning the entire first CSV list.

I am guessing that my issue stems from the output of these CSVs, where each row is an object, and the name of the list is the object key. That is, because I have different names for the lists (and hence different object keys), they all values are being read as different. Is that accurate?

Any tips for how to “dif” these two lists?

hi @aaronkyle I guess you might need to create an array of values in order to compare using Sets which is what is behind d3.difference

You can try this

  d3.difference(
    set1.map(e => e.esmsp_hh_coding_hh_code),
    set2.map(e => e.gis_HH_Code)
  );
1 Like

Thanks @radames! You definitely showed me the way!

Clearly it didn’t come to me that the object keys could be mapped “out” in such a way–leaving just the array for each set as a result, but it totally makes sense! Thank you for pointing this out.

Crazy though how sometimes the documentation will confuse me even more, which is somewhat the case reading the Set docs on MDN. I do, however, appreciate the link and explanation that this is behind d3.difference. Hopefully it’s one of these things that I’ll better grasp after more practice and returning to it repeatedly. :wink: As I am sure you can tell, it’s a long and slow journey for me to learn JavaScript!

Thank you for your time and generous mentoring on this forum!