Farhang Farhang - 2 years ago 72
Javascript Question

isbn de-duplicate with javascript/jquery

Brief background: I retrieve book info from google books and worldcat via each api. I parse through the data to create an html div outputs with stuff of interest for a webapp.

The problem: the isbns. After getting isbns from each, I merge them into one div. For example:

<span id='tada'>9781137012920 9780230339118 9781137012920 1137012927</span>

I would like to eliminate the duplicate isbn (e.g., 9781137012920) with either javascript or jquery. I can get the text of the span but every solution I have tried has failed thus far.

Any help - esp, with a codepen or fiddle - for this amateur would be greatly appreciated.

PS:I know variations of this question have been addressed on stackoverflow - and believe me that I am sick of duplicates! - but I have not managed to fix this after a couple of days of trying. Examples that type in the content as var work but how do I get it from the div and make it work?

Answer Source

Do something like

$('span').text(function(i, v) {
  return v.split(' ') // split the value
    .filter(function(v1, i, arr) {
      return arr.indexOf(v1) == i; // filter unique value
    }).join(' ') // join and return the unique values
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id='tada'>9781137012920 9780230339118 9781137012920 1137012927</span>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download