简体   繁体   English

如何从dict中提取键和值,并使用javascript将它们存储在变量中

[英]How to extract keys and values from a dict and store them in an variable using javascript

I have a dictionary like: 我有一个字典,如:

db_dict={'Col1': ['1', '2', '3', '4'], 'Col2': ['2', '4', '6'], ...}

I want to make a string like this: 我想制作一个这样的字符串:

var simpleQuery = "col1={'1', '2', '3', '4'}&col2={'2', '4', '6'}"

So far I've tried this: 到目前为止,我试过这个:

function getdbdict() {
{ %
    for key, values in dbdict.items %
}
var col = '{{key}}';
var val = '{{values}}';
var colval = col + '=' + '{' + '"' + val + '"' + '}'; { % endfor %
}
var simpleQuery = colval

I am a newbie, Please help me doing this. 我是新手,请帮我这样做。

This'll do it: 这样做:

var db_dict={'Col1': ['1', '2', '3', '4'], 'Col2': ['2', '4', '6']};

var workingArray = []
for (var key in db_dict) {
    if (db_dict[key].length > 0)
        workingArray.push(key.toLowerCase() + "={'" + db_dict[key].join("','") + "'}");
    else
        workingArray.push(key.toLowerCase() + "={}");
}

var simpleQuery = workingArray.join("&");

console.log(simpleQuery);  // col1={'1','2','3','4'}&col2={'2','4','6'} 

Your sample took the original object's key names and made them lowercase in simpleQuery , so I've done the same; 你的样本采用了原始对象的键名,并在simpleQuery中将它们设置为小写,所以我也做了同样的事情。 obviously you can just remove the .toLowerCase() if that isn't needed. 很明显,如果不需要,你可以删除.toLowerCase()

Demo: http://jsfiddle.net/8rZHF/2/ 演示: http//jsfiddle.net/8rZHF/2/

You can try the following 您可以尝试以下方法

db_dict={'Col1': ['1', '2', '3', '4'], 'Col2': ['2', '4', '6']};
var str = "";
for(var key in db_dict){
  str +=key +"={";
  for(var i=0;i<db_dict[key].length; i++){
    str += db_dict[key][i];
    if(i != db_dict[key].length-1)
      str +=",";
  }
  str+="}";
  str+="&";
}
str = str.substring(0, str.length - 1);

You can see it in action here 你可以在这里看到它

I am sure it can be refactored but it is just to give you an idea. 我相信它可以被重构,但它只是给你一个想法。

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

相关问题 JavaScript - 如何使用 RegEx 从响应中提取字符串 - 键/值? - JavaScript - How to extract strings - keys/values from response using RegEx? 如何<span>从数组中</span>提取<span>元素值并将其存储到另一个数组中</span> - how to extract <span> element values from an array and store them into another array.Javascript 你将如何从字符串中提取 2 个数字并将它们存储到一个单独的变量中 - How will you extract 2 numbers and store them into a separate variable from string 如何按dict值分组并存储到javascript中的列表中 - How to group by dict values and store into list in javascript 如何从 Javascript 中的集合数组中提取键的值 - How to extract values of the Keys from an array of collections in Javascript 如何从具有2个文本框的jQuery对话框中获取值? 我可以将它们存储在JavaScript变量中吗? - How can I get values from jQuery dialog box which has 2 text boxes? Can I store them in a JavaScript variable? 如何使用jQuery检索值并将其存储在数组中 - How to retrieve values from json and store them in an array using jQuery 从字符串中提取数字并将其存储在数组中 - Extract numbers from string and store them in array-Javascript 使用变量键访问 JavaScript 个对象中的值 - Using variable keys to access values in JavaScript objects 如何使用不同的键从对象中提取2个值 - How to extract 2 values from an object with different keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM