Please excuse my inability to accurately describe what it is I am after. This in ability has made it very difficult to search for an answer, thus I am asking now.
请原谅我无法准确描述我追求的是什么。这种能力使得搜索答案非常困难,因此我现在要问。
I have an object (data.drivers) which contains a bunch of lap information.
我有一个对象(data.drivers),其中包含一堆圈数信息。
I am looping through this object to update already displayed information, but would like to condense my code and be able to loop through the fields rather than write the same code for every possible field.
我循环遍历此对象以更新已显示的信息,但是希望压缩我的代码并能够循环遍历字段,而不是为每个可能的字段编写相同的代码。
Problem is I do not know how to get the value from i by refering to it with a variable. For example:
问题是我不知道如何通过使用变量引用它来获取i的值。例如:
$.each(data.drivers, function(pos, i) {
var driver_position = i.position; // this and much more would happen for 9 fields
alert (driver_position);
});
This works.
But what I would like to do is this:
但我想做的是:
$.each(data.drivers, function(pos, i) {
var fields = [ 'position', 'name', 'laps', 'lapTime', 'pace', 'difference', 'elapsedTime', 'fastLap', 'eqn' ];
$.each (fields, function (ii, field) {
alert (i.field);
});
});
2 个解决方案
#1
1
What you're looking for is bracket notation:
你要找的是括号表示法:
alert(i[field]);
Though using some more descriptive variable names helps in the long run, quite a bit.
虽然使用一些更具描述性的变量名称有助于长期,但相当多。
#2
1
You could also just loop over properties with each
as well. You don't need that fields array of properties unless there are only specific properties you need and even then you can handle it within the each
.
您也可以使用每个属性循环遍历属性。除非只有您需要的特定属性,否则您不需要该字段属性数组,即使这样您也可以在每个属性中处理它。
// Loop over array of driver objects
$.each(data.drivers, function(index, driver) {
// Loop over driver object properties
$.each(driver, function(key, value) {
// key is property name
// value is property value
});
});
Documentation: http://api.jquery.com/jquery.each/
A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.
通用迭代器函数,可用于无缝迭代对象和数组。具有length属性的数组和类数组对象(例如函数的参数对象)由数字索引迭代,从0到length-1。其他对象通过其命名属性进行迭代。
#1
1
What you're looking for is bracket notation:
你要找的是括号表示法:
alert(i[field]);
Though using some more descriptive variable names helps in the long run, quite a bit.
虽然使用一些更具描述性的变量名称有助于长期,但相当多。
#2
1
You could also just loop over properties with each
as well. You don't need that fields array of properties unless there are only specific properties you need and even then you can handle it within the each
.
您也可以使用每个属性循环遍历属性。除非只有您需要的特定属性,否则您不需要该字段属性数组,即使这样您也可以在每个属性中处理它。
// Loop over array of driver objects
$.each(data.drivers, function(index, driver) {
// Loop over driver object properties
$.each(driver, function(key, value) {
// key is property name
// value is property value
});
});
Documentation: http://api.jquery.com/jquery.each/
A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.
通用迭代器函数,可用于无缝迭代对象和数组。具有length属性的数组和类数组对象(例如函数的参数对象)由数字索引迭代,从0到length-1。其他对象通过其命名属性进行迭代。