Question

I'm in an internship in a small company and I'm only new to jQuery. I've been given this task to work on, where I have to parse the photographer's name from Flickr API and print the user name in a list and also make sure there are no duplications. This is what I have so far,

var list = $("<ul></ul>");// This creates and unordered list.
$.each(data.photos.photo, function(i, set){
  var link = $("<li/>").attr("ownername", set.owner).text(set.ownername);
  var li = $("<li/>").append(link);
  $(list).append(li);
  $("#flickr-users").append(list);
});
var seen = {};
$('ul li').each(function() {
  var txt = $(this).text();
  if (seen[txt])
    $(this).remove();
  else
    seen[txt] = true;
});
console.log(seen); //it shows in the console all usernames which are true and they aren't duplicated

So I'm just struggling to print the names on the HTML file with all the usernames which are set to true.

I would appreciate any help on this.

Was it helpful?

Solution

have a look at jQuery Unique

with 1 line of code you sort and remove duplidates:

var myArray = <your array>;


var  myUniquesArray =jQuery.unique( myArray );

EDIT:

here is a demo Fiddle

EDIT 2:

to merge all your arrays into one array, you can use

jQuery.merge(first, second);

it returns a third array with all the values from first and second

so what you do is basically go over all of your arrays and merge them into your final array:

see another demo Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top