How can I change a data element in a $.get using values from a JSON array?
如何使用JSON数组中的值更改$ .get中的数据元素?
Here's an example of the code that performs the action once:
以下是执行操作一次的代码示例:
$.get(url, {
'p': testerName,
'magic': 'magic', //this value is constant now but may change
'init': init1 //this is the value I want to change from a value in a JSON array
}, function() {
// console.log('done');
});
The value I want to change is "init". For example, if "init1" is in a JSON array similar to:
我想要改变的值是“init”。例如,如果“init1”在JSON数组中,则类似于:
"initvalues":[
{"init1":"usethisfirst"},
{"init1":"usethissecond"},
{"init1":"usethisthird"}
]
I want the $.get
to run three times and stop. Of course, if there are more values in the array, it should use all of them and stop. The count of values is expected to be variable.
我希望$ .get运行三次并停止。当然,如果数组中有更多的值,它应该使用所有值并停止。值的计数预计是可变的。
In the example below, if I named var init1 = usethisfirst
it would run once.
在下面的示例中,如果我将var init1命名为usethisfirst,它将运行一次。
$.get(url, {
'p': testerName,
'magic': 'magic', //this value is constant now but may change
'init': init1 //this is the value I want to change from a value in a JSON array
}, function() {
// console.log('done');
});
And, I could just keep repeating the routine for every different value of "init1" but I know there must be a better way.
并且,我可以继续为“init1”的每个不同值重复该例程,但我知道必须有更好的方法。
Additional Info:
附加信息:
The objective is to avoid hard coding repetitive $.get
functions and use one which will run n number of times driven by the number of values in the JSON array(s).
目标是避免硬编码重复的$ .get函数,并使用一个由JSON数组中的值数驱动n次的函数。
1 个解决方案
#1
2
The simplest way is probably to perform the get request using something like forEach
. If all you want to do is set the init
field from some init1
values, you might try something like this:
最简单的方法可能是使用类似forEach的方法执行get请求。如果您要做的只是从某些init1值设置init字段,您可以尝试这样的事情:
var init_values = [
{"init1" : "use this first"},
{"init1" : "use this second"},
{"init1" : "use this third"}
];
init_values.forEach(function(settings) {
$.get(url, {
'p' : testerName,
'magic' : 'magic',
'init': settings['init1']
}, function() {
// Do something for this particular request.
});
});
However, depending upon the source of your JSON array, you might also be interested in setting the other fields automatically:
但是,根据JSON数组的来源,您可能还有兴趣自动设置其他字段:
var init_values = [
{
"p" : "some value",
"magic" : "magic",
"init" : "use this first"
},
{
"p" : "a different value",
// No value for magic - use default.
"init" : "use this second"
},
{"init" : "use this third"} // Use defaults for 'p' and 'magic'.
];
init_values.forEach(function(settings) {
var defaults = {
"p" : "default p",
"magic" : "default magic",
"init" : "default init"
};
$.get(url, $.extend(defaults, settings), function() {
// Do something for this request.
});
});
#1
2
The simplest way is probably to perform the get request using something like forEach
. If all you want to do is set the init
field from some init1
values, you might try something like this:
最简单的方法可能是使用类似forEach的方法执行get请求。如果您要做的只是从某些init1值设置init字段,您可以尝试这样的事情:
var init_values = [
{"init1" : "use this first"},
{"init1" : "use this second"},
{"init1" : "use this third"}
];
init_values.forEach(function(settings) {
$.get(url, {
'p' : testerName,
'magic' : 'magic',
'init': settings['init1']
}, function() {
// Do something for this particular request.
});
});
However, depending upon the source of your JSON array, you might also be interested in setting the other fields automatically:
但是,根据JSON数组的来源,您可能还有兴趣自动设置其他字段:
var init_values = [
{
"p" : "some value",
"magic" : "magic",
"init" : "use this first"
},
{
"p" : "a different value",
// No value for magic - use default.
"init" : "use this second"
},
{"init" : "use this third"} // Use defaults for 'p' and 'magic'.
];
init_values.forEach(function(settings) {
var defaults = {
"p" : "default p",
"magic" : "default magic",
"init" : "default init"
};
$.get(url, $.extend(defaults, settings), function() {
// Do something for this request.
});
});