再帰処理
3197 ワード
最近、ちょっとボケ気味なのでメモっとく。
children に子要素を再帰的に持つデータに対しての処理関数。
データ構造
let item = {
children = [],
...parameter,
}
let items = [item]
recursive(items, args) {
let return_value = null
items.forEach(item => {
return_value = proc_statement(item, args)
if (!exit_recursive_condition(item, args)) {
// 条件に一致しなければ子要素に対して処理をする
return_value = recursive(item.children, args)
}
});
return return_value
}
exit_recursive_condition(item, args) {
// 再帰処理を抜ける条件
return item.parameter == args
}
proc_statement(item, args) {
// 要素に対する処理
return item
}
args は再帰処理を抜ける条件や要素に対して処理を行うための引数。
全ての要素に対して処理を行うのであれば exit_recursive_condition()は不要でforEach をmapに変えればよいかと。
Author And Source
この問題について(再帰処理), 我々は、より多くの情報をここで見つけました https://qiita.com/sasanoworkz/items/851c154d811247640f0c著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .