2015-12-30 38 views
2

爲什麼sumundefined。我究竟做錯了什麼?不返回對象值之和的遞歸函數

var testData1 = [{ 
 
    "name": "Doctors", 
 
    "category": "Doctors", 
 
    "subCategory": [{ 
 
    "name": "Likes", 
 
    "category": "Likes", 
 
    "subCategory": [{ 
 
     "name": "Visit", 
 
     "category": "Visit", 
 
     "subCategory": [{ 
 
     "name": "Charges", 
 
     "category": "Charges", 
 
     "subCategory": null, 
 
     "val": 30 
 
     }] 
 
    }] 
 
    }, { 
 
    "name": "Dislikes", 
 
    "category": "Dislikes", 
 
    "subCategory": [{ 
 
     "name": "Quality", 
 
     "category": "Quality", 
 
     "subCategory": [{ 
 
     "name": "Appointment", 
 
     "category": "Appointment", 
 
     "subCategory": null, 
 
     "val": 50 
 
     }, { 
 
     "name": "Care", 
 
     "category": "Care", 
 
     "subCategory": null, 
 
     "val": 70 
 
     }, { 
 
     "name": "Attentive ", 
 
     "category": "Attentive ", 
 
     "subCategory": null, 
 
     "val": 90 
 
     }] 
 
    }] 
 
    }, { 
 
    "name": "Neutral", 
 
    "category": "Neutral", 
 
    "subCategory": [{ 
 
     "name": "Professional ", 
 
     "category": "Professional ", 
 
     "subCategory": [{ 
 
     "name": "Ease", 
 
     "category": "Ease", 
 
     "subCategory": null, 
 
     "val": 50 
 
     }, { 
 
     "name": "Competent ", 
 
     "category": "Competent ", 
 
     "subCategory": null, 
 
     "val": 40 
 
     }, { 
 
     "name": "Availability", 
 
     "category": "Availability", 
 
     "subCategory": null, 
 
     "val": 80 
 
     }] 
 
    }] 
 
    }], 
 
    "index": 6 
 
}, { 
 
    "name": "Service", 
 
    "category": "Service", 
 
    "subCategory": [{ 
 
    "name": "Likes", 
 
    "category": "Likes", 
 
    "subCategory": [{ 
 
     "name": "Environment", 
 
     "category": "Environment", 
 
     "subCategory": [{ 
 
     "name": "Professionalism ", 
 
     "category": "Professionalism ", 
 
     "subCategory": null, 
 
     "val": 50 
 
     }, { 
 
     "name": "Room", 
 
     "category": "Room", 
 
     "subCategory": null, 
 
     "val": 30 
 
     }, { 
 
     "name": "Parking", 
 
     "category": "Parking", 
 
     "subCategory": null, 
 
     "val": 20 
 
     }] 
 
    }, { 
 
     "name": "Availability", 
 
     "category": "Availability", 
 
     "subCategory": [{ 
 
     "name": "Competent ", 
 
     "category": "Competent ", 
 
     "subCategory": null, 
 
     "val": 30 
 
     }] 
 
    }] 
 
    }, { 
 
    "name": "Dislikes", 
 
    "category": "Dislikes", 
 
    "subCategory": [{ 
 
     "name": "Management", 
 
     "category": "Management", 
 
     "subCategory": [{ 
 
     "name": "Staff", 
 
     "category": "Staff", 
 
     "subCategory": null, 
 
     "val": 50 
 
     }, { 
 
     "name": "Operations", 
 
     "category": "Operations", 
 
     "subCategory": null, 
 
     "val": 70 
 
     }] 
 
    }, { 
 
     "name": "Nurses", 
 
     "category": "Nurses", 
 
     "subCategory": [{ 
 
     "name": "Medicine", 
 
     "category": "Medicine", 
 
     "subCategory": null, 
 
     "val": 30 
 
     }] 
 
    }] 
 
    }, { 
 
    "name": "Neutral", 
 
    "category": "Neutral", 
 
    "subCategory": [{ 
 
     "name": "Serving", 
 
     "category": "Serving", 
 
     "subCategory": [{ 
 
     "name": "Took long time", 
 
     "category": "Took long time", 
 
     "subCategory": null, 
 
     "val": 50 
 
     }, { 
 
     "name": "Rude", 
 
     "category": "Rude", 
 
     "subCategory": null, 
 
     "val": 40 
 
     }, { 
 
     "name": "Seated", 
 
     "category": "Seated", 
 
     "subCategory": null, 
 
     "val": 80 
 
     }] 
 
    }] 
 
    }], 
 
    "index": 16 
 
}]; 
 

 
function addSum(data) { 
 
    data.forEach(function(d, index) { 
 
    if (Array.isArray(d.subCategory)) { 
 
     return (0 + addSum(d.subCategory)); 
 

 
    } else { 
 

 
     document.write('<pre>' + JSON.stringify(d.val, 0, 4) + '</pre>'); 
 
     return d.val; 
 
    } 
 
    }); 
 
} 
 
var sum = addSum(testData1); 
 
alert(sum);

回答

4

你的回報不屬於addSum,它屬於forEach,這就是爲什麼addSum返回任何(不確定)。
Array.prototype.forEach不期望返回值,不能用於計算總和或別的東西。

您可以利用Array.prototype.reduce實現你的結果:

function sum(arr) 
{ 
    return arr.reduce(function(a, b) { 
    return Array.isArray(b.subCategory) ? a + sum(b.subCategory) : a + b.val; 
    }, 0); 
} 

sum(testData1); 

或者使用ECMAScript 6箭頭功能:

var sum = arr => arr.reduce((a, b) => Array.isArray(b.subCategory) 
    ? a + sum(b.subCategory) 
    : a + b.val, 0); 

工作JSFiddle demo

2

.forEach你不能返回值,這就是爲什麼你undefined,這種情況下更適合.reduce方法

function addSum(data) { 
    return data.reduce(function (p, c) { 
    return p + (Array.isArray(c.subCategory) ? addSum(c.subCategory) : c.val); 
    }, 0); 
} 

Example

,或者如果你想使用.forEach,你可以做這樣的

function addSum(data) { 
    return function sum(data, result) { 
    data.forEach(function (element) { 
     result = Array.isArray(element.subCategory) 
     ? sum(element.subCategory, result) 
     : result + element.val; 
    }); 

    return result; 
    }(data, 0); 
} 

Example

0

你不能從你的foreach返回,只能從你的函數返回。這是你的函數的工作版本:

function addSum(data,tot) { 
    tot = tot || 0; 
    data.forEach(function(d, index) { 
    if (Array.isArray(d.subCategory)) { 
     tot = addSum(d.subCategory, tot); 
    } else { 
     tot += parseInt(d.val); 
     document.write('<pre>' + d.val + ' - ' + tot + ' </pre>'); 
    } 
    }); 
    return tot; 
} 

和的jsfiddle:

https://jsfiddle.net/mckinleymedia/uk5njxnp/

我同意,這應該被簡化爲減少功能,但我認爲這也將有助於怎麼看修改你所做的工作。

0

返回的foreach函數是回調函數,不會從addSum()函數返回。在這種情況下,Foreach回調返回將無濟於事。取而代之的是使用一些外部變量來保持總和,這會起到一定作用

下面更新的代碼定義了addSum()函數中的變量sum,遞歸調用的返回值被添加到它。該函數現在將在最後返回計算的總和。

var testData1 = [{ 
 
    "name": "Doctors", 
 
    "category": "Doctors", 
 
    "subCategory": [{ 
 
    "name": "Likes", 
 
    "category": "Likes", 
 
    "subCategory": [{ 
 
     "name": "Visit", 
 
     "category": "Visit", 
 
     "subCategory": [{ 
 
     "name": "Charges", 
 
     "category": "Charges", 
 
     "subCategory": null, 
 
     "val": 30 
 
     }] 
 
    }] 
 
    }, { 
 
    "name": "Dislikes", 
 
    "category": "Dislikes", 
 
    "subCategory": [{ 
 
     "name": "Quality", 
 
     "category": "Quality", 
 
     "subCategory": [{ 
 
     "name": "Appointment", 
 
     "category": "Appointment", 
 
     "subCategory": null, 
 
     "val": 50 
 
     }, { 
 
     "name": "Care", 
 
     "category": "Care", 
 
     "subCategory": null, 
 
     "val": 70 
 
     }, { 
 
     "name": "Attentive ", 
 
     "category": "Attentive ", 
 
     "subCategory": null, 
 
     "val": 90 
 
     }] 
 
    }] 
 
    }, { 
 
    "name": "Neutral", 
 
    "category": "Neutral", 
 
    "subCategory": [{ 
 
     "name": "Professional ", 
 
     "category": "Professional ", 
 
     "subCategory": [{ 
 
     "name": "Ease", 
 
     "category": "Ease", 
 
     "subCategory": null, 
 
     "val": 50 
 
     }, { 
 
     "name": "Competent ", 
 
     "category": "Competent ", 
 
     "subCategory": null, 
 
     "val": 40 
 
     }, { 
 
     "name": "Availability", 
 
     "category": "Availability", 
 
     "subCategory": null, 
 
     "val": 80 
 
     }] 
 
    }] 
 
    }], 
 
    "index": 6 
 
}, { 
 
    "name": "Service", 
 
    "category": "Service", 
 
    "subCategory": [{ 
 
    "name": "Likes", 
 
    "category": "Likes", 
 
    "subCategory": [{ 
 
     "name": "Environment", 
 
     "category": "Environment", 
 
     "subCategory": [{ 
 
     "name": "Professionalism ", 
 
     "category": "Professionalism ", 
 
     "subCategory": null, 
 
     "val": 50 
 
     }, { 
 
     "name": "Room", 
 
     "category": "Room", 
 
     "subCategory": null, 
 
     "val": 30 
 
     }, { 
 
     "name": "Parking", 
 
     "category": "Parking", 
 
     "subCategory": null, 
 
     "val": 20 
 
     }] 
 
    }, { 
 
     "name": "Availability", 
 
     "category": "Availability", 
 
     "subCategory": [{ 
 
     "name": "Competent ", 
 
     "category": "Competent ", 
 
     "subCategory": null, 
 
     "val": 30 
 
     }] 
 
    }] 
 
    }, { 
 
    "name": "Dislikes", 
 
    "category": "Dislikes", 
 
    "subCategory": [{ 
 
     "name": "Management", 
 
     "category": "Management", 
 
     "subCategory": [{ 
 
     "name": "Staff", 
 
     "category": "Staff", 
 
     "subCategory": null, 
 
     "val": 50 
 
     }, { 
 
     "name": "Operations", 
 
     "category": "Operations", 
 
     "subCategory": null, 
 
     "val": 70 
 
     }] 
 
    }, { 
 
     "name": "Nurses", 
 
     "category": "Nurses", 
 
     "subCategory": [{ 
 
     "name": "Medicine", 
 
     "category": "Medicine", 
 
     "subCategory": null, 
 
     "val": 30 
 
     }] 
 
    }] 
 
    }, { 
 
    "name": "Neutral", 
 
    "category": "Neutral", 
 
    "subCategory": [{ 
 
     "name": "Serving", 
 
     "category": "Serving", 
 
     "subCategory": [{ 
 
     "name": "Took long time", 
 
     "category": "Took long time", 
 
     "subCategory": null, 
 
     "val": 50 
 
     }, { 
 
     "name": "Rude", 
 
     "category": "Rude", 
 
     "subCategory": null, 
 
     "val": 40 
 
     }, { 
 
     "name": "Seated", 
 
     "category": "Seated", 
 
     "subCategory": null, 
 
     "val": 80 
 
     }] 
 
    }] 
 
    }], 
 
    "index": 16 
 
}]; 
 

 
function addSum(data) { 
 
    sum = 0; 
 
    data.forEach(function(d, index) { 
 
    if (Array.isArray(d.subCategory)) { 
 
     sum += (0 + addSum(d.subCategory)); 
 

 
    } else { 
 

 
     document.write('<pre>' + JSON.stringify(d.val, 0, 4) + '</pre>'); 
 
     sum += d.val; 
 
    } 
 
    }); 
 

 
    return sum; 
 
} 
 
var sum1 = addSum(testData1); 
 
alert(sum1);