Trying to remove duplicate info received from an xml file displayed on my html page
I am trying to delete the duplicate atrists that are displayed when I retrieve the info from this xml file using JQuery. How can I accomplish this?
Here is the js file:
$(function(){
$(window).load(function(){
$.ajax({
url: 'http://imaginationeverywhere.info/djronlove/new_2.xml',
dataType: 'xml',
success: function(xml){
$(xml).find("key:contains('Artist') + string").each(function(){
var string1 = $(this).text();
$('<p></p>').addClass('string1').html(string1).appendTo('#container');
});
}
});
});
$('<div></div>').attr('id', 'container').appendTo('body');
});
Here is the html file:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DJ Ron Love Music Catalog</title>
<script开发者_StackOverflow社区 type='text/javascript' src='http://imaginationeverywhere.info/jslib//dev/jquery-1.5.1.js'>
</script>
<script type='text/javascript' src="http://imaginationeverywhere.info/djronlove/itunes.js">
</script>
</head>
<body>
<!--<h3>Songs that currently in DJ Ron Love's Music Catalog</h3>-->
<h3>Artist that DJ Ron Love currently has in roatation</h3>
</body>
</html>
one way you may want to try is by creating a master artist array, and then querying that before adding new artists. You can do this easily using jquerys "inArray" function.
var masterArray = new Array();
$.ajax({
url: 'http://imaginationeverywhere.info/djronlove/new_2.xml',
dataType: 'xml',
success: function(xml){
$(xml).find("key:contains('Artist') + string").each(function(){
var string1 = $(this).text();
if( jQuery.inArray(string1, masterArray) == -1 ){
$('<p></p>').addClass('string1').html(string1).appendTo('#container');
masterArray.push(string1);
}
});
}
});
Note: there may be more efficent ways to do this, but if you're just looking for something simple, that may work.
精彩评论