我有一个像这样运行良好的查询。它返回正确的对象和一切。
var all = new Keen.Query("extraction", {
eventCollection: "android-sample-button-clicks",
timeframe: 'previous_300_minutes'
});
但是我想做的是做这样的事情。看看我如何使用时间变量并将其插入到“timeframe”的查询中?但随后它总是返回未定义的。
if (this.value == "1"){
var time = 'previous_300_minutes'
} else {
var time = 'previous_30_minutes'
}
var all = new Keen.Query("extraction", {
eventCollection: "android-sample-button-clicks",
timeframe: time
});
这总是返回未定义的。我认为这是因为我不能像那样将时间字符串变量插入到查询 JSON 中。我怎样才能插入它以使其格式正确?
最佳答案
试一试:
var time;
if (this.value == "1"){
time = 'previous_300_minutes'
} else {
time = 'previous_30_minutes'
}
var all = new Keen.Query("extraction", {
eventCollection: "android-sample-button-clicks",
timeframe: time
});
或者像这样:
var time = (this.value == "1") ? 'previous_300_minutes' : 'previous_30_minutes';
var all = new Keen.Query("extraction", {
eventCollection: "android-sample-button-clicks",
timeframe: time
});
甚至这样:
var all = new Keen.Query("extraction", {
eventCollection: "android-sample-button-clicks",
timeframe: (this.value == "1") ? 'previous_300_minutes' : 'previous_30_minutes'
});
关于javascript - 如何将可变字符串插入 JSON 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28530201/