I have this code, used to get a number of hours from an object and multiply them by a variable.
Here is the object '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;
and here my function to calculate the pay
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;
}
I have used "hasOwnProperty" to check if the property "hours" exists in work. The result of the function is NaN. Why?
You're already iterating your array via the for
loop - then you do
Object.keys(work).forEach(function(val) {
This doesn't make any sense. work
is an array - not an object. Basically if you remove that line it'll 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;
//});
}
A simpler way may be to use Array.reduce
var totalHourPay = work.reduce(function(total, workDay) {
if (workDay.hasOwnProperty("hours")) {
total += workDay.hours * payPerHour;
}
return total;
}, 0);
You can use Javascript for..in loops like below:
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; }
Why these complicated expressions? Try this ES6 solution:
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.
I believe this is what you wanted:
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.