Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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 - Fatal编程技术网

Javascript 比较值并确定数组中的位置

Javascript 比较值并确定数组中的位置,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有一个名为dataid的数组,下面是我的数组的控制台.log: 在我的URL中,我储存了我的ID: 我想这样做,如果我单击id为“next”的跨距,它会在我的URL中向我发送数组datatid中下一个id的值。因此,首先我们需要确定myid在数组中的位置,然后确定下一个在URL中发送它的值 这就是我所尝试的: var newid = GetURLParameter('myid'); console.log(newid); var dataid = localStorage.ge

我有一个名为dataid的数组,下面是我的数组的
控制台.log

在我的URL中,我储存了我的ID:

我想这样做,如果我单击id为“next”的跨距,它会在我的URL中向我发送数组datatid中下一个id的值。因此,首先我们需要确定myid在数组中的位置,然后确定下一个在URL中发送它的值

这就是我所尝试的:

  var newid = GetURLParameter('myid');
  console.log(newid);
  var dataid = localStorage.getItem("mesid");
  console.log(dataid);


  function nextticket(){
    var position = dataid.indexOf(newid)+1;
    console.log(dataid.indexOf(newid));
    console.log(position);
  }
下面是我得到的:


但立场是错误的

localStorage存储字符串,而不是数组。 输出中的位置10是因为“4653”出现在字符串“547548314653,…”中的字符位置10处

您需要将字符串拆分回一个数组,如下所示:

var position = dataid.split(',').indexOf(newid) + 1;

此处使用的
split()
方法将逗号分隔的字符串转换为数组。

localStorage存储字符串,而不是数组。 输出中的位置10是因为“4653”出现在字符串“547548314653,…”中的字符位置10处

您需要将字符串拆分回一个数组,如下所示:

var position = dataid.split(',').indexOf(newid) + 1;

此处使用的
split()
方法将逗号分隔的字符串转换为数组。

请向我们展示您的尝试。。。请看一看
Array.prototype.indexOf()
方法。@cl3m我重新编辑我的帖子,向您展示我的尝试请展示您的尝试。。。请看一下
Array.prototype.indexOf()
方法。@cl3m我重新编辑了我的帖子,向您展示了我的尝试