[英]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.