I have a method that takes as parameter an array of several depth (group
) and a name as a string (currentFilterName
)
checkFilterValue(group, currentFilterName) {
group.forEach((element, index) => {
if (element.children) {
if (element.name === currentFilterName) {
this.returnedValue = element.value
} else {
this.checkFilterValue(element.children || this.filtersConfig[index + 1], currentFilterName)
}
} else {
if (element.name === currentFilterName) {
this.returnedValue = element.value
}
}
})
}
The depth levels are the element.children
, being the goal is to return the element.value
, at each turn if the element has a depth level I first check if element.name === currentFilterName
if it is the case I would like returned the element.value
if it is not the case I return the method evening with the next element.children
if there is otherwise the original tables with the next index this.filtersConfig [index + 1]
, if the element has no depth I simply check if element.name === currentFilterName
and I return to element.value
The problem I have is that the method is a little verbose, how can we simplify it with an elegant writing?
strict
mode? Why do you want to setreturnedValue
this way? \$\endgroup\$