简体   繁体   中英

Remove objects from a javascript array

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM