[英]I am trying to read an array coming from python(database mongodb), and read it in a context menu submenu
So, I am trying to read the array coming from python and python is calling mongodb. 因此,我正在尝试读取来自python的数组,而python正在调用mongodb。 As I am trying to read the array, I am getting each character from the array. 当我尝试读取数组时,我正在从数组中获取每个字符。 Like the code is reading the array as a string. 就像代码将数组读取为字符串一样。
I have tried reading this code in various ways but always getting stuck. 我试图以各种方式阅读此代码,但始终会卡住。 First, I am reading the value and putting it in a const variable. 首先,我正在读取值并将其放入const变量中。 Then I am creating a for loop to loop through the array and display each item like '1 - client 1'... 然后我创建一个for循环以遍历数组并显示每个项目,例如'1-client 1'...
var displayClient = [];
const clients = document.getElementById("clients").value;
var clients_array = clients;
console.log(clients);
var itemDisp = [];
var arrayLength = clients_array.length;
//looping through the objects inside the object
for (var i = 0; i < arrayLength; i++) {
var client = clients_array[i].toString().split(',');
displayClient.push(client[0] + ' - ' + client[1]);
itemDisp.push({ label: displayClient[i]});
}
//calling to diplay in the context menu
return {
"AddClient" : {
label: "Add Client",
"submenu": itemDisp,
disabled: false
}
//the array which I am trying to read
[['1', 'client 1'], ['2', 'client 2']]
the expected result is, to display something like; 预期的结果是,显示类似的东西; 1 - client 1... While the actual result is; 1-客户1 ...实际结果是; [ - undefined, [ - undefined, ' - undefined... [-未定义,[-未定义,'-未定义...
try this: 尝试这个:
return {
"AddClient" : {
label: "Add Client",
"submenu": itemDisp,
disabled: false
}
}
I make this and work: 我做这个工作:
var displayClient = [];
const clients = [['1', 'client 1'], ['2', 'client 2']];
var clients_array = clients;
console.log(clients);
var itemDisp = [];
var arrayLength = clients_array.length;
//looping through the objects inside the object
for (var i = 0; i < arrayLength; i++) {
var client = clients_array[i].toString().split(',');
displayClient.push(client[0] + ' - ' + client[1]);
itemDisp.push({ label: displayClient[i]});
}
//calling to diplay in the context menu
let test = {
"AddClient" : {
label: "Add Client",
"submenu": itemDisp,
disabled: false
}
}
console.log(test);
console.log(itemDisp);
I am not able to understand your code but I might understood your problem. 我无法理解您的代码,但我可能理解您的问题。 This code can help you get the desired output :- 此代码可以帮助您获得所需的输出:-
` `
var itemDisp = [];
const clients = document.getElementById("clients").value;
try{
// This line will parse your array string into JS array object
var clientsArray = JSON.parse(clients.replace(/'/g,"\""));
for(var internalArray of clientsArray){
itemDisp.push({label : internalArray.join(" - ")});
}
}catch(e){
console.error("String provided is not a valid array string");
}
console.log(itemDisp);
` `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.