Are there any jquery features to query multi-dimensional arrays in a similar fashion to the DOM?

前端 未结 3 542
有刺的猬
有刺的猬 2021-01-04 21:12

What the question says...

Does jQuery have any methods that will allow you to query a mult-dimensional array of objects in a similar fashion as it does with the DOM.

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 21:57

    You can't use selector syntax, but jQuery comes with $.grep and $.inArray, which can be useful for this. grep returns a new array of elements that match a predicate. inArray returns the index of the first matching element, or -1. For instance:

    var matches = $.grep(array, function(el){
      return el.StartOfPeriod > 2000;
    });
    

    These are similar to the standard ECMAScript methods, Array.filter (simimlar to grep) and Array.indexOf (similar to inArray); jQuery actually uses Array.indexOf where available. There are also other useful ECMAScript methods, such as Array.every (all elements matching) and Array.some (at least one matching). MDC has code you can add to your project so these work in browsers that don't have native implementations.

提交回复
热议问题