简体   繁体   English

从javascript数组中删除对象

[英]Remove objects from a javascript array

I have an array that is populated using some xpath; 我有一个使用一些xpath填充的数组; so the items in the array could be as little as 1-20+. 所以数组中的项目可能只有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 但是,每个xml有点不同,即

<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'.) 我需要的是只抓住后两项(包含'duedate'和'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? 什么是最好的方法来遍历xml中可能存在的所有标记,然后只抓住具有duedates和availdates的标记并将它们放入数组中?

You want a different selector: http://jsfiddle.net/mKUxw/ . 你想要一个不同的选择器: http//jsfiddle.net/mKUxw/ This one selects only <data> items which have both a <duedate> and <availdate> : 这其中只选择<data>项,其既具有<duedate><availdate>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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