Remove objects from a javascript array

+2 votes
45 views
asked Dec 13, 2012 in java script by jonathan (3,105 points)

 

I have an array that is populated using some xpath; so the items in the array could be as little as 1-20+.

I am populating it like so:

    var masterPath = $('items item', response.responseXML)
    masterPath.each(function (index, obj) {
    var assignments = $('> data > title', obj).text();
    courseHwork.push(assignments);

However, the xml is a bit different for each, ie

<data>
  <title>example</title>
</data>

<data>
  <title>example2</title>
  <duedate>03-21-12</duedate>
</data>

<data>
  <title>example3</title>
  <duedate>05-02-12</duedate>
  <availdate>04-01-12</availdate>
<data>

What I need is to only grab the latter two items (the 'data' that contains both a 'duedate' AND an 'availdate'.)

What would be the best way to go through all tags that may exist in the xml, and then only grab the ones with duedates and availdates and put them into the array?

1 Answer

0 votes
answered Dec 13, 2012 by arthi (1,110 points)

You want a different selector: http://jsfiddle.net/mKUxw/. This one selects only <data> items which have both a <duedate> and <availdate>:

$("> data:has(duedate):has(availdate) > title", obj)
...