简体   繁体   English

因此,我试图使用for循环从数组中获取一组项目并将其显示到上下文菜单子菜单中,但是以某种方式,我无法

[英]So I am trying to get a set of items from an array using a for loop and displaying them into a context menu sub menu, but somehow I am not able to

So I am trying to get a list of clients from an array and then display them one by one into a context menu sub menu. 因此,我试图从数组中获取客户端列表,然后将其一个一个地显示在上下文菜单子菜单中。 But somehow I am confused on how to do it. 但是,我对如何做到这一点感到困惑。 Any help would be really appreciated. 任何帮助将非常感激。

So below is the code that I have already tried but I am always getting the last item from the list like this, while I would like to get all the items from the list one under the other. 因此,下面是我已经尝试过的代码,但是我总是像这样从列表中获取最后一个项目,而我想从列表中获取另一个项目。

action: function () {
    var itemDisp = [];  
    var client;
    var arrayLength = clients_array.length;
    for (var i = 0; i < arrayLength; i++) {
        client = clients_array[i].toString().split(',');
        displayClient.push(client[0] + ' - ' + client[1]);
        clientDisp = client[0] + ' - ' + client[1];
        itemDisp = { label: displayClient[i]};   
    }
} 
return { 
    "AddClient" : {
        label: "Add Client",                        
        "submenu": { 
            itemDisp
        }
    }
}

Right now I am getting the last item from the array with the above code, while I would like to get all the items found in the array. 现在,我要使用上面的代码从数组中获取最后一个项目,而我想获取数组中找到的所有项目。

It looks like you need an array. 看起来您需要一个数组。 However in your code you are overwriting the array with an object. 但是,在代码中,您正在用对象覆盖数组。 This might explain why you only see the last object. 这也许可以解释为什么只看到最后一个对象。

Instead try this: 而是尝试以下方法:

function () {
    var itemDisp = [];  
    var client;
    var arrayLength = clients_array.length;
    for (var i = 0; i < arrayLength; i++) {
        client = clients_array[i].toString().split(',');
        displayClient.push(client[0] + ' - ' + client[1]);
        clientDisp = client[0] + ' - ' + client[1];

        // Important part here!
        itemDisp.push({ label: displayClient[i]});
    }
} 
return { 
    "AddClient" : {
        label: "Add Client",   
        // Also changed. We just pass the array.                     
        "submenu": itemDisp
    }
}

By using the array push method we add the object to the end of the itemDisp array. 通过使用数组push方法,我们将对象添加到itemDisp数组的末尾。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我目前正在尝试从显示菜单项的数组中返回一些数据到我的搜索栏中 - I am currently trying to return some data from an array displaying menu items into my search bar 我正在尝试创建一个下拉菜单,用于存储餐馆菜单中的项目,使用vanilla JavaScript传递一组对象 - I am trying to create a drop down menu which stores items from a restaurant menu, using vanilla JavaScript by passing it an array of objects 我有一个菜单和一个子菜单,但是我无法访问子菜单,但是我想访问子菜单以获取适当的菜单 - I have one menu and sub menu's but I am not able to access sub menu but i want to access sub menu for appropriate menu 我正在尝试读取来自python(database mongodb)的数组,并在上下文菜单子菜单中读取它 - I am trying to read an array coming from python(database mongodb), and read it in a context menu submenu 我正在尝试从操作填充子菜单,但似乎总是说未定义 - I am trying to populate sub menu from action but seems like its always saying undefined 我正在尝试调用 switch_func function。 但不知何故无法得到想要的结果 - I am trying to call the switch_func function. But somehow not able to get the desired result 我试图遍历对象数组并将其填充到表中 - I am trying to loop through an array of objects and populate them to a table 我正在尝试隐藏显示菜单 <a><img></a> <a>用jQuery</a> - I am trying to hide show menu from an <a><img> with jquery 我正在尝试将用户输入作为 JSON 数组存储在本地存储中,并将它们显示在 HTML 表中 - I am trying to store user input as JSON array in local storage and displaying them in HTML table 我正在尝试从数组 object 的这个 for 循环中显示我的图像 - I am trying to get my image to display from this for loop from the array object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM