如何将逗号分隔字符串设置为参数
How to set a comma sep. string as a paramter?
可能重复:
如何获取查询字符串值?
我有一个函数调用,比如:
var ex = _.where(obj, {param1:0,param2:1});
它运行良好,所有代码都在后面。
我检查我的URL,取出参数,并将这些信息保存到一个数组中:
var query = window.location.search.substring(1);
var vars = query.split("&");
var testParamter = vars.toString().replace( /=/g,':' );
使用console.log(testParamter)返回的;以下结果:
param1:0,param2:1
但是我还不能将var testParamter插入到我的函数调用中:
var ex = _.where(obj, {testParamter});
因为它是一个字符串,我不能用eval()处理它。有人能告诉我解决问题的正确方法吗?
感谢
遍历querystring中的值,并根据这些值创建一个对象:
var query = window.location.search.substring(1),
vars = query.split("&"),
params = {};
$.each(vars, function(k,v) {
var arr = v.split('=');
params[ decode(arr[0]) ] = decode(arr[1]);
});
var ex = _.where(obj, params);
function decode(s) {
return decodeURIComponent(s.replace(/'+/g, " "));
}
相关文章:
- 将字符串的一部分设置为数组的一部分
- 将localStorage字符串设置为Integer
- 从 UTC 字符串设置日期格式
- 如何根据 URL 中符号后的第一个字符串设置变量
- 从本地存储 base64 字符串设置背景图像
- 从类似 csv 的字符串设置文本字段值
- 将 CodeMirror 子字符串设置为粗体而不更改模式
- jQuery:从参数字符串设置表单状态
- 如何使用字符串设置局部变量
- 按数字和字符串设置新的 Date() 会给出不同的结果
- 将字符串设置为类似日期的格式
- 避免基于特定URL字符串设置cookie
- 如何在将javascript字符串设置为从Django变量传播的值时转义引号
- 使用完全限定字符串设置JSON属性
- 需要根据模型数据中的字符串设置复选框
- 使用来自java类的字符串设置输入值
- 使用包含英国格式日期的字符串设置new Date()
- CRM 2011:使用javascript从字符串设置日期时间字段
- 将一个HTML字符串设置为等于javascript中的一个变量
- 如何将逗号分隔字符串设置为参数