简体   繁体   English

我正在尝试读取来自python(database mongodb)的数组,并在上下文菜单子菜单中读取它

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

相关问题 因此,我试图使用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 我正在尝试从数据库中获取 map mydata 但我收到此错误。 TypeError:无法读取未定义的属性“地图” - I am trying to map mydata from the database but i am getting this error. TypeError: Cannot read property 'map' of undefined 我正在尝试将该数组写入HTML,但它不断出现未定义的状态 - I am trying to write the array to HTML but it keeps coming up as undefined MongoDB-无法从数据库读取数据 - MongoDB - Can't read data from the database 我正在尝试更新 object 数组的属性,但它显示无法分配给 object 的只读属性“isSelected” - I am trying to update a property of an array of object but it is showing Cannot assign to read only property 'isSelected' of object 我正在尝试使用数组作为数据库 - I am trying to use array as database 上下文菜单“无法读取菜单属性”Angular 7 - Context Menu 'Cannot Read Property of Menu' Angular 7 我目前正在尝试从显示菜单项的数组中返回一些数据到我的搜索栏中 - I am currently trying to return some data from an array displaying menu items into my search bar 我正在使用 selectpicker 我想编辑来自数据库的数据,但所有选项都已被选中 - I am using selectpicker I want to edit the data coming from the database, but all options are coming already selected 我无法从xml读取数据 - I am unable to read data from xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM