需要通过从php转换到jquery的数组来获得索引。

时间:2022-08-08 14:25:00

our 10 year old software used to be written in PHP, combined with HTML and SQL snippets. I am forced to make the best out of it.

我们10年前的软件是用PHP编写的,结合了HTML和SQL代码片段。我不得不充分利用它。

Having no experience at all with JavaScript, I have to use jQuery's inArray to get the index by value.

我没有使用JavaScript的经验,所以必须使用jQuery的inArray来按值获取索引。

Although everything seems to be right, inArray returns -1 all the time, although I should get the proper index. So what have I done wrong?

尽管一切似乎都是对的,inArray始终返回-1,尽管我应该得到适当的索引。我做错了什么?

$html1 .= "<script src='/js/jquery-3.2.1.min.js'> </script>
<script>  var juseridarray = ".json_encode($useridarray).";

$(document).ready(function()
{
    $('#chiefname').change(UpdateInfo);
    $('#chiefid').change(UpdateInfo);                                                                                            
    $('#chiefs').change(UpdateInfo);
});
function UpdateInfo()
{
    var chiefname = $('#chiefname').val();                                                                                      
    var chiefid = $.inArray( chiefname, juseridarray);
    var chief = chiefid;
    $('#chief').val(chief);
    console.log(juseridarray);
    console.log(chiefname);
}

</script>";

Excuse me, I forgot to mention the log output. So this is a snippet of juseridarray log output which I copied from firebug:

不好意思,我忘了提到日志输出。这是我从firebug中复制的juseridarray输出片段

Object[430]
1: "Administrator"
3: "Bob"
8: "Wegner, Meike "
10: "Uhrenbauer, Sonja"
[426 more...]

对象[430]1:“管理员”3:“Bob”8:“Wegner, Meike”10:“Uhrenbauer, Sonja”[426…]

The array seems to have an correct index and correct values. So when I search for 'Bob', inArray returns -1, although 'Bob' is expected to return 3. On the other hand, I get 'Bob' in return when using

数组似乎有一个正确的索引和正确的值。所以当我搜索'Bob'时,inArray返回-1,尽管'Bob'预计返回3。另一方面,我在使用时也会得到“Bob”作为回报

console.log(juseridarray[3]);

The array content is static. It doesn't change in runtime. And if it would, usernames would always keep their id (stored as index)

数组内容是静态的。它在运行时不会改变。如果是的话,用户名将始终保留他们的id(存储为索引)

1 个解决方案

#1


1  

First of all, it is not an array you are operating with, it's an object.

首先,它不是你正在操作的数组,它是一个对象。

You cannot use inArray of an object like you intend to do. The code below should however, return the id, of the user with the given username, if it exists.

不能像您打算的那样使用对象的数组。但是,下面的代码应该返回给定用户名的用户id(如果存在的话)。

function findUserIdByUsername(user, userArray){
     var user_id = false;
     $.each(userArray, function(id, username){ 
          if( user == username ) user_id = id
     });
     return user_id;
}

var chiedif = findUserIdByUsername('bob', juseridarray);

#1


1  

First of all, it is not an array you are operating with, it's an object.

首先,它不是你正在操作的数组,它是一个对象。

You cannot use inArray of an object like you intend to do. The code below should however, return the id, of the user with the given username, if it exists.

不能像您打算的那样使用对象的数组。但是,下面的代码应该返回给定用户名的用户id(如果存在的话)。

function findUserIdByUsername(user, userArray){
     var user_id = false;
     $.each(userArray, function(id, username){ 
          if( user == username ) user_id = id
     });
     return user_id;
}

var chiedif = findUserIdByUsername('bob', juseridarray);