1

I am new to jquery and stuck in a problem and need help from experts.

here is my code

 for (val in list) {
    var result = $.grep(node, function(e){
          return e.id === list[val].Id;
    });
    if (result === 0)
     // do something
}

When i try to pass JSLint on my I got this error

#1 Don't make functions within a loop.

I know the problem that i am making a function in the jquery grep function But i am stuck as how to move my function out side the loop assign it to a variable and then call it. As my function is talking an argument e. I have tried it like this

var Visible = function (e, list, val) {return e.id === list[val].Id;}; 

for (val in list) {
   var result = $.grep(node, Visible(e, list, val));
}

but now jslint is giving an error that

 #1 'e' was used before it was defined.
    var result = $.grep(node, Visible(e)); // Line 125, Pos 69

//--------------------------------------------------------------------

here is the data

node: Array[5]
  0: HTMLTableRowElement
  1: HTMLTableRowElement 
  2: HTMLTableRowElement
  3: HTMLTableRowElement

list: Array[5]
  0: Object
    0: "cc"
    1: "ss"
    Id: "000"
  1: Object
  2: Object

Now I am finding that for which element of the the list an html element exist. If for any element in the list and htmlelement does not exist then i r=create one i am checking it by result === 0 what can be the solution to this problem. Any help will be appreciated thanks

4

4 回答 4

2

When you write Visible(e) you call function visible with argument e. This is not what you want. You want to give the function itself to .grep(). So try fixing it like this:

var result = $.grep(node, Visible);
于 2012-12-25T19:30:45.613 回答
0

You have to pass a function to $.grep. But Visible(e, list, val) is trying to call the function and passing the return value to $.grep (which is either true or false). At that moment, e is not defined yet (it's an argument which $.grep is supposed to pass to the callback).

Instead, you have to create a function which returns a function that does the comparison:

function createCallback(id) {
    return function(e) {
        return e.id === id}; 
    };
}

for (val in list) {
   var result = $.grep(node, createCallback(list[val].Id));
}

That said, creating functions in a loop is ok as long as those functions are called before the next iteration of the loop, but JSLint is very picky.


A better solution could to remove all elements from the array for which elements already exist:

$.map(list, function() {
    return $('#' + this.Id).length === 1 ? null : this;
}).each(function() {
    // create and append element here
    // e.g.
    $('<div />', {id: this.Id}).appendTo('#container');
});
于 2012-12-25T19:31:10.963 回答
0

A far simpler approach is to simply create an ID selector and check if it exists:

for (val in list) {    
    if( $('#'+list[val].Id).length){
        /* element exists in page*/
    }       
}

To look within node as collection of elements only can use filter() method

var $node=$(node);
for (val in list) {    
     if( $node.filter('#'+list[val].Id).length){
            /* element exists in node*/
     }       
 }
于 2012-12-25T19:37:49.563 回答
0

You do not need jQuery for this at all. Just use two loops, and check what you want:

for (var i=0; i<list.length; i++) {
    var found = false;    
    for (var j=0; j<node.length; j++) {    
        if(list[i].id == node[j].Id) {
            // We have found such element in node!
            found = true;
            break; 
        }
    }     
    if(!found) {
        // we did not find such element in node
    }
}

I suggest you to understand how basic Javascript loops work, and only after that use jQuery functions for such tasks. It will help you.

于 2012-12-25T20:20:21.113 回答