根据数组中的某些条件按数组元素分组

时间:2015-09-01 12:46:12

标签: javascript jquery

我有50个元素的数组,每个元素都有一些记录类型。我需要使用jquery过滤类似记录类型的元素。我该怎么做。

请帮忙

1 个答案:

答案 0 :(得分:0)

要通过简单属性划分类似元素,您可以使用Bucket sort

$othsimlar_prices = "select ABS(exshowroom - ".$price_query['minprice'].") as minprice,
                            ABS(exshowroom - ".$price_query['maxprice'].") as maxprice,
                            make,
                            model 
                    from ncp_variant_cache 
                    order by maxprice";

这将产生以下输出:

//Elements to divide into buckets according to their foo value:
var elems = [
  {foo: 1, bar: 23}
, {foo: 2, bar: 24}
, {foo: 1, bar: 25}
, {foo: 2, bar: 26}
, {foo: 2, bar: 27}
, {foo: 3, bar: 29}
];
//Sorting elements into buckets depending on foo:
var buckets = {};
for(var i = 0; i < elems.length; i++){
  var elem = elems[i];
  if(elem.foo in buckets){
    buckets[elem.foo].push(elem);
  }else{
    buckets[elem.foo] = [elem];
  }
}
//Iterating buckets to display output:
var keys = Object.keys(buckets);
for(var i = 0; i < keys.length; i++){
  var key = keys[i];
  console.log(key+' => '+JSON.stringify(buckets[key]));
}