最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stopping the Jquery.map when a condition is met - Stack Overflow

programmeradmin2浏览0评论

I have the following code:

var parentEls = node.parents() 
    .map(function () {

        var curParentID = this.getAttribute("id"); 
        var curParentClass = this.getAttribute("class");

        if(curParentID) {
            return this.tagName + "#" + curParentID;
            /*stop the map function from proceeding*/
        } else {
            return this.tagName;
        }
    })
    .get().reverse().join(", ");

The above code helps to find search for unique id and once an ID is spotted , it creates the xpath. How can i stop the the map function once it has e to a point where the id is retrieved and the map function should stop? I tried return false and break but it does not work.

Is there any other suggestion?

I have the following code:

var parentEls = node.parents() 
    .map(function () {

        var curParentID = this.getAttribute("id"); 
        var curParentClass = this.getAttribute("class");

        if(curParentID) {
            return this.tagName + "#" + curParentID;
            /*stop the map function from proceeding*/
        } else {
            return this.tagName;
        }
    })
    .get().reverse().join(", ");

The above code helps to find search for unique id and once an ID is spotted , it creates the xpath. How can i stop the the map function once it has e to a point where the id is retrieved and the map function should stop? I tried return false and break but it does not work.

Is there any other suggestion?

Share Improve this question edited Aug 12, 2013 at 6:43 Darkzaelus 2,0951 gold badge16 silver badges31 bronze badges asked Aug 12, 2013 at 6:37 madimadi 5,6625 gold badges39 silver badges48 bronze badges 2
  • 1 Just use .parentsUntil('[id]') so you only have the tree up to the closest parent with an id attribute. – pawel Commented Aug 12, 2013 at 7:18
  • Could you give an example by answering? people can vote you suggestions if it is logical :) – madi Commented Aug 12, 2013 at 7:21
Add a ment  | 

2 Answers 2

Reset to default 4

I don't think it is possible using .map(), you may have to use .each() here

var temp = [];
node.parents().each(function () {
    var curParentID = this.getAttribute("id"); 
    var curParentClass = this.getAttribute("class"); 
    if(curParentID){
        temp.push(this.tagName + "#" + curParentID);
        return false;
    } else {
        temp.push(this.tagName);
    }
});
var parentEls = temp.reverse().join(", ");
   // parentsUntil() doesn't include the element specified by selector, 
   // so you need to add the closest ancestor with id attribute to the collection
   var parentEls = node.parentsUntil('[id]').add( node.closest('[id]') )
        .map(function () {
            return this.id ? '#' + this.id : this.tagName;
        })
        .get().join(", ");

Fiddle (updated): http://jsfiddle/SKqhc/5/

发布评论

评论列表(0)

  1. 暂无评论