3
  <Item id="419" rank="0.0000" date_added="2012-09-24T19:43:14" date_end="2012-10-15T19:43:14" uid="134333" price="  ,-" district="-1" district_text="Friend" cid="9" cid_text="Underholdning" img_height_thumb="433" />
  <Item id="418" rank="0.0000" date_added="2012-09-24T19:17:42" date_end="2012-10-15T19:17:42" uid="134332" price="  ,-" district="-1" district_text="Friend" cid="9" cid_text="Underholdning" img_height_thumb="254" />
  <Item id="405" rank="102.0000" date_added="2012-09-23T18:55:20" date_end="2012-10-14T18:55:20" uid="134331" price="  102,-" district="-1" district_text="Friend" cid="761" cid_text="Mote" img_height_thumb="280" />

This is sample xml data. And I am using this code to get xml.

 xmlDoc = xmlhttp.responseXML;
 var items = xmlDoc.getElementsByTagName("Item");

//Want to add code

for (i = 0; i < items.length && i < 40; i++) {
var bRank = items[i].getAttribute("rank");
}

//want to add code

I want to filter all data rank which is greater then 0. And I want to add filter only in this palce so my rest code won't change. So how could i filter items variable and get filter values in other variable. so the loop don't get effected. I can change the name of variables in for loop.

Please suggest me any idea for this.

First Edit

xmlDoc = xmlhttp.responseXML;
        var items1 = xmlDoc.getElementsByTagName("Item");

        var items = $(items1).filter('Item').each(function () {
            var bRank = $(this).attr('rank');
            var tempRank = bRank.replace("0000", "").replace(".", "");
            if (tempRank > 0)
                return this;
        });

alert(items.length);
        alert(items1.length);

Answer of both alert is 3.

but answer should like this for first alert 1 and for second alert 3.

but i am getting same response each alert. please check and let me know your suggestion. Thanks

1
  • Please check first edit. Commented Oct 23, 2012 at 7:27

2 Answers 2

1

Try this,

Live Demo

var items = $('item').filter(function(){
  if(parseFloat($(this).attr('rank')) > 0)
      return this;
});

for (i = 0; i < items.length && i < 40; i++) {
   var bRank = items[i].getAttribute("rank");
}

You will need to add the xml to DOM to apply filter on xml. after that you can remove it.

Live Demo

$('#div1').html(xmlDoc); 
var items = $('#div1 item').filter(function(){
  if(parseFloat($(this).attr('rank')) > 0)
      return this;
});
$('#div1').remove();

for (i = 0; i < items.length && i < 40; i++) {
   var bRank = items[i].getAttribute("rank");
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for answer but in my case it's returning 0.
I have updated my answer, you need to put it in DOM to get the results. Give it a try.
Thanks for answer. still getting 0. I am using other js files. is this a reason for 0.
Do you have div1 in the html of page you getting the response or you are adding in some other element then div1?
Adding same div with same id.
|
0
var filteredArray = [];

for (i = 0; i < items.length && i < 40; i++) {
  if(parseFloat(items[i].getAttribute("rank"), 10) > 0) {
    filteredArray.push(items[i]);
  }
}

filteredArray will then contain all items with rank > 0.

5 Comments

Thanks for answer. I think append is not working. can you please check it and suggest me.
@PankajMishra Oops, sorry, it's .push not .append.
push is not working. if i use this "filteredArray.push(items[i]);" Then I can't use for loop like that. I can't use this code "var bRank = items[i].getAttribute("rank");". Please check and suggest me.
@PankajMishra, can you post the exact complete code you are using?
for (i = 0; i < filteredArray.length && i < 40; i++) { var bRank = filteredArray[i].getAttribute("rank"); } Please check this code. This is not working if i am using push.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.