jQuery/Javascript显示从到的结果

jQuery/Javascript显示从到的结果,javascript,jquery,arrays,substr,Javascript,Jquery,Arrays,Substr,这是使用该线程的javascript/jQuery重写的新问题,因为我不能直接在MySQL查询中这样做 所以问题很简单: 我将从MySQL查询接收到的数组转换为javascript数组: var array = ["m2", "1", "2", "11", "12", "m4", "m3", "m5", "17", "m1"]; 并且需要从数组值中接受参数的函数来显示。例如: function showCategories(marker){ } 因此,在上面的示例中,如果我调用showCa

这是使用该线程的javascript/jQuery重写的新问题,因为我不能直接在MySQL查询中这样做

所以问题很简单:

我将从MySQL查询接收到的数组转换为javascript数组:

var array = ["m2", "1", "2", "11", "12", "m4", "m3", "m5", "17", "m1"];
并且需要从数组值中接受参数的函数来显示。例如:

function showCategories(marker){

}
因此,在上面的示例中,如果我调用showCategoriesm2;我需要得到这个:

1
2
11
12
17
如果我叫showCategoriesm5;我需要得到这个:

1
2
11
12
17
我目前正在尝试substr并在第一个示例中找到begin m2的索引,找到最后一个m索引,即m4,并从数组中删除其他项。有简单的方法吗?

变量数组=[m2,1,2,11,12,m4,m3,m5,17,m1]; 函数showCategoriesmarker{ 设currentCat='n'; return array.reduceobj,item=>{ 如果item.indexOfm==-1{ obj[currentCat].pushitem; }否则{ currentCat=项目; obj[currentCat]=[]; } 返回obj; },{}[marker]| |[]; } console.logshowCategoriesm2; console.logshowCategoriesm5 变量数组=[m2,1,2,11,12,m4,m3,m5,17,m1]; 函数showCategoriesmarker{ 设currentCat='n'; return array.reduceobj,item=>{ 如果item.indexOfm==-1{ obj[currentCat].pushitem; }否则{ currentCat=项目; obj[currentCat]=[]; } 返回obj; },{}[marker]| |[]; } console.logshowCategoriesm2;
console.logshowCategoriesm5 找到标记的索引,将其后的所有内容切片,然后找到该数组中的第一个m索引,并将其切片。剩下的只是中间的数字

变量数组=[m2,1,2,11,12,m4,m3,m5,17,m1]; 函数showCategoriesmarker{ 让start=array.indexOfmarker; 如果开始==-1返回[]; 让slicedArray=array.slicestart+1; 让end=slicedArray.findIndexval=>val.startsWithm; 返回slicedArray.slice0,结束; } console.logshowCategoriesm2;
console.logshowCategoriesm5 找到标记的索引,将其后的所有内容切片,然后找到该数组中的第一个m索引,并将其切片。剩下的只是中间的数字

变量数组=[m2,1,2,11,12,m4,m3,m5,17,m1]; 函数showCategoriesmarker{ 让start=array.indexOfmarker; 如果开始==-1返回[]; 让slicedArray=array.slicestart+1; 让end=slicedArray.findIndexval=>val.startsWithm; 返回slicedArray.slice0,结束; } console.logshowCategoriesm2;
console.logshowCategoriesm5 我想你可以循环找到所需的索引并将结果存储到子数组中

一种方法是循环,直到通过执行以下操作找到m2:

function showCategories(find)
{
  let returnArray = [];
  let start = null;
  if (array.includes(find) //checks if the element exists
  {
    for(int i = 0; i < array.length; i++)
    {
      if (start == null && array[i] == "m2") //looks for element
      {
        start = i;
      }
      else if(isNaN(Number(array[i]))) //pushes element into array
      {
        returnArray.push(array[i]);
      }
      else if(start != null && !isNaN(Number(array[i]))) //breaks if element after find is not an int
      {
        break;
      }
    }
  }
  return returnArray;
}

我还没有对此进行测试,但是沿着这些思路进行的一些操作会起作用。

我认为您可以循环查找所需的索引,并将结果存储到子数组中

一种方法是循环,直到通过执行以下操作找到m2:

function showCategories(find)
{
  let returnArray = [];
  let start = null;
  if (array.includes(find) //checks if the element exists
  {
    for(int i = 0; i < array.length; i++)
    {
      if (start == null && array[i] == "m2") //looks for element
      {
        start = i;
      }
      else if(isNaN(Number(array[i]))) //pushes element into array
      {
        returnArray.push(array[i]);
      }
      else if(start != null && !isNaN(Number(array[i]))) //breaks if element after find is not an int
      {
        break;
      }
    }
  }
  return returnArray;
}

我还没有测试过这个,但是按照这些思路做的一些东西会起作用。

太棒了!简单的,非常有用的…我希望MySQL查询能做到这一点…但是它不能。所以这个JavaScript函数将过滤结果并做它需要的…谢谢…@ USS2631534如果性能是一个问题,你可能要考虑这个减少在一个大数据集上存储的内存量。一般来说,减少的价值在于您能够回顾以前存储在累加器中的项目,这是不必要的或正在进行的here@asthmatic-如果这真的是一个问题,我们会做一次减少,并将其保存到一个外部变量。这里不是一个真正的问题。太好了!简单的,非常有用的…我希望MySQL查询能做到这一点…但是它不能。所以这个JavaScript函数将过滤结果并做它需要的…谢谢…@ USS2631534如果性能是一个问题,你可能要考虑这个减少在一个大数据集上存储的内存量。一般来说,减少的价值在于您能够回顾以前存储在累加器中的项目,这是不必要的或正在进行的here@asthmatic-如果这真的是一个问题,我们会做一次减少,并将其保存到一个外部var。这不是一个真正的问题。