Javascript 单数组到关联数组

发布于 2024-09-14 21:52:19 字数 502 浏览 0 评论 0原文

我正在尝试构建一个 JavaScript,它将根据数组中的客户名称自动完成文本字段,但我还想为客户 ID 设置一个隐藏字段。我不确定如何构建一个关联数组来完成此任务。我找到了一堆用于自动完成的片段,但我正在努力解决如何构建数组并随后引用它来设置 2 个 html 标签:

<SCRIPT language="JavaScript">
function autocomplete(filter)
{
var filter = 1,one,2,two,3,three
}
</SCRIPT>
    <input type='hidden' id='id' />
    <input type='text' id='custname' -onKeypress='autocomplete();' />

编辑:我调用的函数有一个传递给它的单个数组来执行自动完成。因此,如果我能弄清楚如何从过滤器变量中的 Evens 构建一个新数组,并从 Odds 构建另一个数组,我应该没问题。

I'm trying to build a javascript that will autocomplete a textfield based off of a customer name in an array, but I would also like to set a hidden field to the customer ID. Im not sure how to build an associative array that will allow me to accomplish this. I've found a bunch of sniplets for autocomplete, but I'm struggling on how to build the array and subsequently referencing it to set 2 html tags:

<SCRIPT language="JavaScript">
function autocomplete(filter)
{
var filter = 1,one,2,two,3,three
}
</SCRIPT>
    <input type='hidden' id='id' />
    <input type='text' id='custname' -onKeypress='autocomplete();' />

Edit: The function I am calling has a single array passed to it to perform the autocomple. So If I can figure out how to build a new array from the the Evens and another from the Odds in the filter variable, I should be okay.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生生不灭 2024-09-21 21:52:19

假设您要将 [1,one,2,two,3, Three] 转换为 {one: 1, Two: 2, Three: 3}

function makeAssociative(array)
{
    var associative;
    if(array.length % 2 == 0)
    {
        associative = {};
        for(int i = 0; i<array.length; i += 2)
        {
            associative[i+1] = associative[i];
        }
    }

    return associative;
}

Assuming that you want to turn [1,one,2,two,3,three] into {one: 1, two: 2, three: 3}:

function makeAssociative(array)
{
    var associative;
    if(array.length % 2 == 0)
    {
        associative = {};
        for(int i = 0; i<array.length; i += 2)
        {
            associative[i+1] = associative[i];
        }
    }

    return associative;
}
再可℃爱ぅ一点好了 2024-09-21 21:52:19
var customers = {}; 
customers["1"] = "Name 1"; 
customers["2"] = "Name 2"; 
customers["3"] = "name 3";

    // iterating
for (i in customers) {
    alert (i);      // key
    alert (customers[i]);   // value
}

您可以在以下页面找到详细说明和示例:
关联数组

var customers = {}; 
customers["1"] = "Name 1"; 
customers["2"] = "Name 2"; 
customers["3"] = "name 3";

    // iterating
for (i in customers) {
    alert (i);      // key
    alert (customers[i]);   // value
}

You can find detailed descriptions and examples on the following page:
Associative arrays

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文