我有一个多维数组,我试图循环。我希望循环最终查看第一个对象(在示例代码中为"pressure"
),然后对第一个对象内的每个对象执行特定操作。
但我甚至无法让它正常工作。我知道我的$.each
功能是完全错误的 - 我无法弄清楚如何使它正确。当然,我在控制台里什么都没得到。
$(document).ready(function() {
var units = {
"pressure": [
{
short: "atm",
val: "1",
long: "Atmosphere (standard)"
}, {
short: "bar",
val: "1.01325027383089",
long: "Bar"
}, {
short: "cmHg",
val: "76.0000526890144",
long: "Centimeter of Mercury (0℃)"
}, {
short: "cmH2O",
val: "1033.25183423631",
long: "Centimeter of Water (4℃)"
}, {
short: "ft-H2O",
val: "33.899503811341",
long: "Foot of Water (4℃)"
}, {
short: "GPa",
val: "0.00010132502738",
long: "Gigapascal"
}, {
short: "HPa",
val: "1013.25027383088",
long: "Hectopascal"
}, {
short: "inHg",
val: "29.9212805862261",
long: "Inch of Mercury (0℃)"
}, {
short: "inH2O",
val: "406.793640435982",
long: "Inch of Water (4℃)"
}, {
short: "kPa",
val: "101.325027383089",
long: "Kilopascal"
}, {
short: "MPa",
val: "0.10132502738309",
long: "MegaPascal"
}, {
short: "mbar",
val: "1013.25027383088",
long: "Millibar"
}, {
short: "mtorr",
val: "760000.526890144",
long: "Millitorr"
}, {
short: "mmHg",
val: "760.000205385833",
long: "Millimeter of Mercury (0℃)"
}, {
short: "mmH2O",
val: "10332.5183423631",
long: "Millimeter of Water (4℃)"
}, {
short: "Pa",
val: "101325.027383088",
long: "Pascal"
}, {
short: "psf",
val: "2116.21372690676",
long: "Pound-Force/Square-Foot"
}, {
short: "psi",
val: "14.6959793215884",
long: "Pound-Force/Square-Inch"
}, {
short: "Torr",
val: "760.000526890144",
long: "Torr"
}
],
"density": [
{
short: "lb/in3",
val: "1",
long: "Pound / Inch-Cubed"
}, {
short: "lb/ft3",
val: "1728.00001173627",
long: "Pound / Foot-Cubed"
}, {
short: "slug/ft3",
val: "53.707964051111",
long: "Slug / Foot-Cubed"
}, {
short: "grain/ft3",
val: "12094392.8271733",
long: "Grain / Foot-Cubed"
}, {
short: "grain/in3",
val: "6999.99988928036",
long: "Grain / Inch-Cubed"
}, {
short: "g/cm3",
val: "27.679904710267",
long: "Gram / Centimeter-Cubed"
}, {
short: "g/cup",
val: "6.548788655679",
long: "Gram / Cup"
}, {
short: "g/L",
val: "27679.9047102671",
long: "Gram / Liter"
}, {
short: "kg/cm3",
val: "0.02767990471",
long: "Kilogram / Centimeter-Cubed"
}, {
short: "kg/L",
val: "27.679904710267",
long: "Kilogram / Liter"
}, {
short: "kg/m3",
val: "27679.9047102671",
long: "Kilogram / Meter-Cubed"
}
]
};
$.each(units, function(index, obj) {
$.each(obj, function(short, val) {
console.log(obj.index.short);
console.log(obj.index.val);
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 0 :(得分:3)
您可能想要这样做:
Object.keys(units).forEach((key, index) => { // loop over the units keys
units[key].forEach(object => { // loop over the objects in arrays
// do your stuff with the objects
});
});
答案 1 :(得分:3)
$.each(units, function(index, obj) {
console.log(index)
$.each(obj, function(short, val ) {
console.log(val.short);
console.log(val.val);
});
});
答案 2 :(得分:2)
使用.
语法引用对象属性时,您尝试访问 literal 属性。
例如,obj.index.short
。这会查找名为index
的属性,然后查找名为short
的属性。我假设您希望使用index
变量,以便您访问正确的属性。
类似于:obj[index].short
。但这不起作用,因为您的obj[index]
值是一个数组。
你想要做的是遍历外部对象,然后遍历每个属性(这是一个数组)并获取每个数组元素(对象)的值。
试试这个:
$.each(units, function(index, obj){
// `index` is either "pressure" or "density"
// `obj` is the value of that property (an array)
$.each(obj, function(short, val){
// `short` is the index in the array
// `val` is each element in the array, it's an object
console.log(val.short);
console.log(val.val);
});
});
答案 3 :(得分:1)
$(document).ready(function() {
var units = {
"pressure": [
{
short: "atm",
val: "1",
long: "Atmosphere (standard)"
}, {
short: "bar",
val: "1.01325027383089",
long: "Bar"
}, {
short: "cmHg",
val: "76.0000526890144",
long: "Centimeter of Mercury (0℃)"
}, {
short: "cmH2O",
val: "1033.25183423631",
long: "Centimeter of Water (4℃)"
}, {
short: "ft-H2O",
val: "33.899503811341",
long: "Foot of Water (4℃)"
}, {
short: "GPa",
val: "0.00010132502738",
long: "Gigapascal"
}, {
short: "HPa",
val: "1013.25027383088",
long: "Hectopascal"
}, {
short: "inHg",
val: "29.9212805862261",
long: "Inch of Mercury (0℃)"
}, {
short: "inH2O",
val: "406.793640435982",
long: "Inch of Water (4℃)"
}, {
short: "kPa",
val: "101.325027383089",
long: "Kilopascal"
}, {
short: "MPa",
val: "0.10132502738309",
long: "MegaPascal"
}, {
short: "mbar",
val: "1013.25027383088",
long: "Millibar"
}, {
short: "mtorr",
val: "760000.526890144",
long: "Millitorr"
}, {
short: "mmHg",
val: "760.000205385833",
long: "Millimeter of Mercury (0℃)"
}, {
short: "mmH2O",
val: "10332.5183423631",
long: "Millimeter of Water (4℃)"
}, {
short: "Pa",
val: "101325.027383088",
long: "Pascal"
}, {
short: "psf",
val: "2116.21372690676",
long: "Pound-Force/Square-Foot"
}, {
short: "psi",
val: "14.6959793215884",
long: "Pound-Force/Square-Inch"
}, {
short: "Torr",
val: "760.000526890144",
long: "Torr"
}
],
"density": [
{
short: "lb/in3",
val: "1",
long: "Pound / Inch-Cubed"
}, {
short: "lb/ft3",
val: "1728.00001173627",
long: "Pound / Foot-Cubed"
}, {
short: "slug/ft3",
val: "53.707964051111",
long: "Slug / Foot-Cubed"
}, {
short: "grain/ft3",
val: "12094392.8271733",
long: "Grain / Foot-Cubed"
}, {
short: "grain/in3",
val: "6999.99988928036",
long: "Grain / Inch-Cubed"
}, {
short: "g/cm3",
val: "27.679904710267",
long: "Gram / Centimeter-Cubed"
}, {
short: "g/cup",
val: "6.548788655679",
long: "Gram / Cup"
}, {
short: "g/L",
val: "27679.9047102671",
long: "Gram / Liter"
}, {
short: "kg/cm3",
val: "0.02767990471",
long: "Kilogram / Centimeter-Cubed"
}, {
short: "kg/L",
val: "27.679904710267",
long: "Kilogram / Liter"
}, {
short: "kg/m3",
val: "27679.9047102671",
long: "Kilogram / Meter-Cubed"
}
]
};
$.each(units, function(i, obj) {
console.log(i);
$.each(obj, function(j, obj2) {
console.log(obj2.short +' : '+obj2.val+' , '+obj2.long);
});
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
答案 4 :(得分:1)
单位是一个对象,压力是这个对象的关键
var _getPressure=units.pressure // will return value of key pressure
_getPressure.forEach(function(item){ // use Array forEach to loop through array
document.write('<pre>'+item.short+'--'+item.val+'</pre>')
})