Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-使用数组传递变量_Javascript_Jquery_Arrays_Performance_Sorting - Fatal编程技术网

Javascript-使用数组传递变量

Javascript-使用数组传递变量,javascript,jquery,arrays,performance,sorting,Javascript,Jquery,Arrays,Performance,Sorting,我正在使用一些我从中改编的代码,但有些东西我不太明白最好的方法是什么。我试图使用不同的排序函数简化一些代码,这些函数将特定值的排序应用于列表项数组 此时,函数根据特定的因子进行比较,然后返回值进行排序 我想通过这个数组/排序调用传递另外两个变量,但我似乎无法找到编写这个函数的方法。目前,我正在通过在窗口上显示全局变量的方式进行操作,但我宁愿直接传递变量 根据以下代码,任何拧紧和清理的方法都将不胜感激: arr = []; sort_func = $j(this).children(':selec

我正在使用一些我从中改编的代码,但有些东西我不太明白最好的方法是什么。我试图使用不同的排序函数简化一些代码,这些函数将特定值的排序应用于列表项数组

此时,函数根据特定的因子进行比较,然后返回值进行排序

我想通过这个数组/排序调用传递另外两个变量,但我似乎无法找到编写这个函数的方法。目前,我正在通过在窗口上显示全局变量的方式进行操作,但我宁愿直接传递变量

根据以下代码,任何拧紧和清理的方法都将不胜感激:

arr = [];
sort_func = $j(this).children(':selected').val();

$j('li.collectionItem').each(function(){
    arr.push(this);
});

if (sort_func == "a_z")
{
      window.dataType = 'alpha';
      window.bigFirst = false;
      arr.sort(sort_products);
}
else if (sort_func == "z_a")
{
      window.dataType = 'alpha';
      window.bigFirst = true;
      arr.sort(sort_products);
}


// custom sort functions
function sort_products(a, b)
{
  dataType = window.dataType;
  bigFirst = window.bigFirst;

  var compA = $j(a).data(dataType);
  var compB = $j(b).data(dataType);

  if (bigFirst == true)
  {
    return (compA > compB) ? -1 : (compA < compB ) ? 1 : 0;
  }
  else
  {
    return (compA < compB) ? -1 : (compA > compB ) ? 1 : 0;
  }
}
arr=[];
sort_func=$j(this).children(':selected').val();
$j('li.collectionItem')。每个(函数(){
arr.push(这个);
});
if(sort_func==“a_z”)
{
window.dataType='alpha';
window.bigFirst=false;
arr.sort(分拣产品);
}
else if(sort_func==“z_a”)
{
window.dataType='alpha';
window.bigFirst=true;
arr.sort(分拣产品);
}
//自定义排序函数
功能分类产品(a、b)
{
dataType=window.dataType;
bigFirst=window.bigFirst;
var compA=$j(a).数据(数据类型);
var compB=$j(b).数据(数据类型);
if(bigFirst==true)
{
返回(compA>compB)?-1:(compAcompB)?1:0;
}
}

您可以在另一个函数中包装原始的
产品排序
,如下所示:

function sort_products(dataType, bigFirst)
{
  return function (a, b)
  {
    var compA = $j(a).data(dataType);
    var compB = $j(b).data(dataType);

    if (bigFirst == true)
    {
      return (compA > compB) ? -1 : (compA < compB ) ? 1 : 0;
    }
    else
    {
      return (compA < compB) ? -1 : (compA > compB ) ? 1 : 0;
    }
  }
}
if (sort_func == "a_z")
{
  arr.sort(sort_products('alpha', false));
}
else if (sort_func == "z_a")
{
  arr.sort(sort_products('alpha', true));
}

我不知道您有多少个元素,但是如果您避免在comparator函数中调用那些jQuery(假设这就是
$j
)的话,它会加快速度

var arr = []; // You really need to declare your variables!
var sort_func = $j(this).children(':selected').val();
var sortInfo = {
  'a_z': {type: 'alpha', ascending: true},
  'z_a': {type: 'alpha', ascending: false},
  // ... whatever the other types are
}[sort_func];

$j('li.collectionItem').each(function(){
  arr.push({ elem: this, key: $j(this).data(sortInfo.type) });
});

arr.sort(function(a, b) {
  return (sortInfo.ascending ? 1 : -1) *
    a.key > b.key ? 1 : a.key < b.key ? -1 : 0;
});

// transform the array into an array of just the DOM nodes
for (var i = 0; i < arr.length; ++i)
  arr[i] = arr[i].elem;
var arr=[];//你真的需要声明你的变量!
var sort_func=$j(this).children(':selected').val();
变量sortInfo={
'a_z':{type'alpha',升序:true},
'z_a':{type'alpha',升序:false},
//…不管其他类型是什么
}[排序函数];
$j('li.collectionItem')。每个(函数(){
arr.push({elem:this,key:$j(this.data(sortInfo.type)});
});
arr.sort(函数(a,b){
返回(排序信息升序?1:-1)*
a、 key>b.key?1:a.key
谢谢,伙计,这里有好东西。就范围变量达成一致。出于兴趣,这是速度问题还是合规问题?@LetterAfterZ嗯,主要是性能问题。比较例程将被调用很多次,因此尽可能降低它的成本对于较大的列表会有所不同。