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

Javascript efficient search array for value with jQuery - Stack Overflow

programmeradmin3浏览0评论

There's a gap in my JavaScript knowledge here. I want to search an array of objects values for a particular value and return it.

For the year I have been writing JavaScript, I have been implementing it like this:

var itemClicked = (function(){

  var retval;

  //Note self.inventory.itemsArray is an array of JS objects

  $(self.inventory.itemsArray).each(function(i){
    if(parseInt(this.id) === parseInt(idOfItem)){
      retval = this;
      return false;
    }
  });

  return retval;

})();

It works, but I'm sure as anything there is a more elegant way. Tell me please!

EDIT - Solution

Thanks to @gdoron with his answer below.

var myVar = $(self.owner.itemsArray).filter(function(){
   return parseInt(this.id) == parseInt(recItemID);
}).get(0);

Note: .get(0) was added at the end because myVar is wrapped as a jQuery object.

There's a gap in my JavaScript knowledge here. I want to search an array of objects values for a particular value and return it.

For the year I have been writing JavaScript, I have been implementing it like this:

var itemClicked = (function(){

  var retval;

  //Note self.inventory.itemsArray is an array of JS objects

  $(self.inventory.itemsArray).each(function(i){
    if(parseInt(this.id) === parseInt(idOfItem)){
      retval = this;
      return false;
    }
  });

  return retval;

})();

It works, but I'm sure as anything there is a more elegant way. Tell me please!

EDIT - Solution

Thanks to @gdoron with his answer below.

var myVar = $(self.owner.itemsArray).filter(function(){
   return parseInt(this.id) == parseInt(recItemID);
}).get(0);

Note: .get(0) was added at the end because myVar is wrapped as a jQuery object.

Share Improve this question edited Jan 9, 2013 at 23:08 Adam Waite asked Jan 9, 2013 at 22:25 Adam WaiteAdam Waite 18.9k23 gold badges130 silver badges149 bronze badges 9
  • Define "more efficient." Some will misunderstand with "less characters" – Alexander Commented Jan 9, 2013 at 22:28
  • 1 So self.inventory.itemsArray is an array of selectors or jQuery objects? – elclanrs Commented Jan 9, 2013 at 22:30
  • why not index your array by id? They you just need to do return self.inventory.itemsArray[id] and avoid the loop entirely. – Patrick Gunderson Commented Jan 9, 2013 at 22:33
  • What do you mean with efficient? If you're looking for performance, you might want to sort the array by id and perform a binary search to achieve O(n log n) time complexity - or even switch to a hash map for O(n) by storing the items by id. If you're just looking to write less code, use $.filter(). – Mattias Buelens Commented Jan 9, 2013 at 22:33
  • It's an array of Javascript objects. More efficient as in, less characters and not having to execute potentially useless code in a loop until it's found – Adam Waite Commented Jan 9, 2013 at 22:33
 |  Show 4 more comments

4 Answers 4

Reset to default 15

The native jQuery function for this is filter:

$(data).filter(function(){
    return this.id == "foo";
});

It's shorter than code you have and more important a lot more readable.
About efficiency, it will iterate all the elements in the set to find as much as possible matches, but I hardly believe it will be the bottle neck of your application, don't focus on micro-optimisations.

I suggest you read Eric Lipper blog about Which is faster.

You can also use grep as suggested by @Mattias Buelens:

$.grep(data, function(ele){
    retun ele.id == "foo";
});

Just another option using jQuery's $.grep( ) function

var arr = $.grep( self.inventory.itemsArray, function ( n ) { 
    return n.id == idOfItem;
});

The above returns an array of matching array elements. If you just want the first it is easy enough to return arr[0] if it exists.

Although I'm unsure what the function is actually supposed to do (due to the external contexts' variables), the following should be more efficient cycle-wise

var itemClicked = (function(){

  var i, array = self.inventory.itemsArray, length = array.length;

  for( i=0; i < length; i++) {

    if(parseInt(array[i].id) === parseInt(idOfItem)){
      return array[i];
    }

  }

  return undefined;

})();

It's an array of Javascript objects

Then do not use jQuery at all. At least, use $.each instead of building a wrapper object around the array. Still, a simple for-loop is much shorter and more performant:

var itemClicked = (function(idnum) {
    var arr = self.inventory.itemsArray;
    for (var i=0, l=arr.length; i<l; i++)
        if (parseInt(arr[i].id, 10) === idnum)
            return arr[i];
})( parseInt(idOfItem, 10) );

You might as well think of storing the id properties as numbers right away, so you don't need to convert it each time.

发布评论

评论列表(0)

  1. 暂无评论