Javascript 如何在递归for循环函数中返回值?
这是密码Javascript 如何在递归for循环函数中返回值?,javascript,algorithm,for-loop,recursion,search,Javascript,Algorithm,For Loop,Recursion,Search,这是密码 var temp = [ { slugs: ['men'], children: [ { slugs: ['men', 'tops'], children: [ { slugs: ['men', 'tops', 'shirts'] }, { slugs: ['men', 'tops', 'swe'] } ] }, { slugs: ['men', 'bottoms'], ch
var temp = [
{
slugs: ['men'],
children: [
{ slugs: ['men', 'tops'], children: [
{ slugs: ['men', 'tops', 'shirts'] },
{ slugs: ['men', 'tops', 'swe'] }
] },
{
slugs: ['men', 'bottoms'], children: [
{ slugs: ['men', 'bottoms', 'pants'] },
{ slugs: ['men', 'bottoms', 'under'] }
]
}
]
},
{
slugs: ['women'],
children: [
{ slugs: ['women', 'tops'], children: [
{ slugs: ['women', 'tops', 'shirts'] },
{ slugs: ['women', 'tops', 'swe'] }
] },
{
slugs: ['women', 'bottoms'], children: [
{ slugs: ['women', 'bottoms', 'pants'] },
{ slugs: ['women', 'bottoms', 'under'] }
]
}
]
}
]
function matchTreeObj (tree, location) {
if (_.isArray(tree) && tree.length > 0) {
for(let i=0;i<tree.length;i++){
matchTreeObj(tree[i], location)
}
}
if (tree.slugs && (tree.slugs.join('/') === location)) {
console.log(tree)
return tree
} else if (tree.children && tree.children.length > 0) {
matchTreeObj(tree.children, location)
}
}
const aaa = matchTreeObj(temp, 'men/tops')
console.log('from log', aaa)
var-temp=[
{
鼻涕虫:[男人],
儿童:[
{鼻涕虫:['男人','顶'],孩子们:[
{鼻涕虫:[“男人”、“上衣”、“衬衫”]],
{鼻涕虫:['men','top','swe']}
] },
{
鼻涕虫:['男人','屁股'],孩子们:[
{鼻涕虫:[“男人”、“屁股”、“裤子”]],
{鼻涕虫:['男人','屁股','下面]]
]
}
]
},
{
鼻涕虫:[“女人”],
儿童:[
{鼻涕虫:['妇女','顶尖'],儿童:[
{slugs:['women','top','shirts']],
{鼻涕虫:['women','top','swe']}
] },
{
鼻涕虫:['女人','屁股'],孩子们:[
{鼻涕虫:[“女人”、“屁股”、“裤子”]],
{鼻涕虫:['女人','屁股','下面]]
]
}
]
}
]
函数matchTreeObj(树,位置){
if(u.isArray(tree)和&tree.length>0){
for(设i=0;i=0){
matchTreeObj(tree.children,位置)
}
}
常数aaa=matchTreeObj(临时“男/女”)
console.log('from log',aaa)
它使用的是lodash,在这里。代码基本上输出其
slug
值与位置参数匹配的对象块。我可以让控制台日志正常工作。但我无法获取返回的数据。(我已经尝试返回递归使用的函数,但仍然无法工作。)
我做错了什么?在
for
循环中迭代数组时,检查调用matchTreeObj
的输出-如果它是真的,返回它:
for(let i=0;i<tree.length;i++){
const result = matchTreeObj(tree[i], location)
if (result) return result;
}
var-temp=[
{
鼻涕虫:[男人],
儿童:[
{鼻涕虫:['男人','顶'],孩子们:[
{鼻涕虫:[“男人”、“上衣”、“衬衫”]],
{鼻涕虫:['men','top','swe']}
] },
{
鼻涕虫:['男人','屁股'],孩子们:[
{鼻涕虫:[“男人”、“屁股”、“裤子”]],
{鼻涕虫:['男人','屁股','下面]]
]
}
]
},
{
鼻涕虫:[“女人”],
儿童:[
{鼻涕虫:['妇女','顶尖'],儿童:[
{slugs:['women','top','shirts']],
{鼻涕虫:['women','top','swe']}
] },
{
鼻涕虫:['女人','屁股'],孩子们:[
{鼻涕虫:[“女人”、“屁股”、“裤子”]],
{鼻涕虫:['女人','屁股','下面]]
]
}
]
}
]
函数matchTreeObj(树,位置){
if(u.isArray(tree)和&tree.length>0){
for(设i=0;i=0){
返回matchTreeObj(tree.children,位置)
}
}
常数aaa=matchTreeObj(临时“男/女”)
console.log('from log',aaa)
在for
循环中迭代数组时,检查调用matchTreeObj
的输出-如果它是真的,则返回它:
for(let i=0;i<tree.length;i++){
const result = matchTreeObj(tree[i], location)
if (result) return result;
}
var-temp=[
{
鼻涕虫:[男人],
儿童:[
{鼻涕虫:['男人','顶'],孩子们:[
{鼻涕虫:[“男人”、“上衣”、“衬衫”]],
{鼻涕虫:['men','top','swe']}
] },
{
鼻涕虫:['男人','屁股'],孩子们:[
{鼻涕虫:[“男人”、“屁股”、“裤子”]],
{鼻涕虫:['男人','屁股','下面]]
]
}
]
},
{
鼻涕虫:[“女人”],
儿童:[
{鼻涕虫:['妇女','顶尖'],儿童:[
{slugs:['women','top','shirts']],
{鼻涕虫:['women','top','swe']}
] },
{
鼻涕虫:['女人','屁股'],孩子们:[
{鼻涕虫:[“女人”、“屁股”、“裤子”]],
{鼻涕虫:['女人','屁股','下面]]
]
}
]
}
]
函数matchTreeObj(树,位置){
if(u.isArray(tree)和&tree.length>0){
for(设i=0;i=0){
返回matchTreeObj(tree.children,位置)
}
}
常数aaa=matchTreeObj(临时“男/女”)
console.log('from log',aaa)
学习的好机会–在数学和计算机科学中,相互递归是递归的一种形式,其中两个数学或计算对象(如函数或数据类型)相互定义。相互递归在函数编程和一些问题域(如递归)中非常常见rsive下降分析器,其中数据类型自然是相互递归的——
type node =
{ slugs : string array
, children : node array
}
每个节点都有一个children属性,它是一个节点数组,每个节点都有一个包含更多节点的children属性。因此,这里有一个递归数据结构,是应用相互递归技术的好时机
下面,match
调用match1
,然后调用match
。这两个函数都是使用纯表达式编写的。不需要for
循环,因为for
是一个语句,依赖于副作用,并且以命令式风格使用。函数式样式避免副作用,因此支持就地递归对于循环的)
const None =
Symbol ()
const EmptyNode =
{ slugs: []
, children: []
}
const match1 = (s = "", node = EmptyNode) =>
node.slugs .join ('/') === s
? node
: match (s, node.children)
const match = (s = "", [ node = None, ...rest ] = []) =>
node === None
? undefined
: match1 (s, node) || match (s, rest)
console .log
( match ("men/tops", data) // { slugs: [ men, tops ], ... }
, match ("women/bottoms", data) // { slugs: [ women, bottoms ], ... }
, match ("cat/dog", data) // undefined
)
一个小的调整允许我们收集所有结果,而不是在第一场比赛后停止-
嘿,看,我们返回了一个值,甚至没有使用
return
。谢谢,函数式。这是一个很好的学习机会–在数学和计算机科学中,相互递归是一种递归形式,其中两个数学或计算对象(如函数或数据类型)是根据彼此定义的ursion在函数式编程和一些问题领域中非常常见,例如递归下降解析器,其中数据类型自然是相互递归的——
type node =
{ slugs : string array
, children : node array
}
每个节点都有一个children属性,它是一个节点数组,每个节点都有一个包含更多节点的children属性。因此,这里有一个递归数据结构,是应用相互递归技术的好时机
下面,match
调用match1
,然后调用match
。这两个函数都是使用纯表达式编写的。不需要for
循环,因为for
是一个语句,依赖于副作用,并且以命令式样式使用。Funct