我有这段代码,用于从对象获取数小时并将它们乘以变量。
这是对象' work'
var work = [
{'day': 27, 'hours': 7.30},
{'day': 29, 'hours': 7.30},
{'day': 31, 'hours': 10},
{'day': 1, 'hours': 8.30},
{'day': 2, 'hours': 7},
{'day': 3, 'hours': 7},
{'day': 5, 'hours': 7.30},
{'day': 6, 'hours': 7},
{'day': 7, 'hours': 7.30},
{'day': 8, 'hours': 8},
{'day': 9, 'hours': 9.30}
]
var payPerHour = 7;
这里是我计算工资的函数
function calculatePay()
{
var result = 0, fResult = 0;
for(var i = 0; i < work.length; i++) {
Object.keys(work).forEach(function (val) {
if (work[i].hasOwnProperty('hours'))
result = work[i][val] * payPerHour;
fResult += result;
});
}
return fResult;
}
我用过&#34; hasOwnProperty&#34;检查财产是否&#34;小时&#34;存在于工作中。该函数的结果是NaN。为什么呢?
答案 0 :(得分:4)
你已经通过for
循环迭代了你的数组 - 然后你做了
Object.keys(work).forEach(function(val) {
这没有任何意义。 work
是一个数组 - 不是对象。基本上,如果删除该行,它将起作用:
for(var i = 0; i < work.length; i++) {
//Object.keys(work).forEach(function(val) {
if(work[i].hasOwnProperty('hours'))
result = work[i]["hours"] * payPerHour; //use the right property here
fResult += result;
//});
}
更简单的方法可能是使用Array.reduce
var totalHourPay = work.reduce(function(total, workDay) {
if (workDay.hasOwnProperty("hours")) {
total += workDay.hours * payPerHour;
}
return total;
}, 0);
答案 1 :(得分:0)
您可以使用Javascript for..in循环,如下所示:
function calculatePay() {
var result = 0, fResult = 0;
for(var index in work) {
if(work[index].hasOwnProperty('hours'))
result = work[index]['hours'] * payPerHour;
fResult += result;
}
return fResult;
}
答案 2 :(得分:0)
为什么这些复杂的表达?试试这个ES6解决方案:
var work = [{'day':27,'hours':7.30},{'day':29,'hours':7.30},{'day':31,'hours':10},
{'day':1,'hours':8.30},{'day':2,'hours':7},{'day':3,'hours':7},
{'day':5,'hours':7.30},{'day':6,'hours':7},{'day':7,'hours':7.30},
{'day':8,'hours':8},{'day': 9, 'hours': 9.30}],
calculatePay=(b)=>work.reduce((c,d)=>c+d.hours*b,0)
// Test
console.log( calculatePay(7) ) // Returns 600.6 and 7 is your rate per hour
// Or redefine function calculatePay this way if you need ES5 compatibility
// function calculatePay(a) {
// return work.reduce(function(b,c) {
// return b + c.hours * a
// }, 0)
// }
// And if you really needs use hasOwnProperty, define it this way
// function calculatePay(a) {
// return work.reduce(function(b,c) {
// return b + (c.hasOwnProperty('hours') && c.hours || 0) * a
// }, 0)
// }
// But you dont need hasOwnProperty. You are processing simple array,
// not object. And this array is full of ordinary object literals. None of
// them does not inherits anything from another object, unless you override
// Object itself.
答案 3 :(得分:0)
我相信这就是你想要的:
var work = [{'day': 27, 'hours': 7.30},
{'day': 29, 'hours': 7.30},
{'day': 31, 'hours': 10},
{'day': 1, 'hours': 8.30},
{'day': 2, 'hours': 7},
{'day': 3, 'hours': 7},
{'day': 5, 'hours': 7.30},
{'day': 6, 'hours': 7},
{'day': 7, 'hours': 7.30},
{'day': 8, 'hours': 8},
{'day': 9, 'hours': 9.30}
];
var payPerHour = 7;
var result = 0, fResult = 0;
function calculatePay(){
for(var i = 0; i < work.length; i++) {
Object.keys(work).forEach(function() {
result = work[i].hours * payPerHour;
fResult += result;
});
}
console.log("Final total: " +fResult);
}
calculatePay();