【发布时间】:2018-04-13 08:26:22
【问题描述】:
现在我实现了在用户组中查找用户的功能。这些是嵌套组,因此搜索是通过递归调用实现的。我的代码在下面列出;我也使用 redux。
我使用searchGroupUserAcc调用search来获取包含指定用户的目标组。我已经确认参数targetGroup被正确赋值了,但是不知道为什么搜索完成后它的值还是{}。
我很困惑;欢迎所有想法。谢谢。
function search(groups)(groups, tokenAcc, targetGroup){
...
//search in a group; g is one group is groups
for(let i=0;i<g.userList.length;++i) {
if(g.userList[i]===tokenAcc) {
//confirmed that targetGroup's value is not{}
targetGroup=JSON.parse(JSON.stringify(g));
return;
}
}
//for searching subGroups
search(...);
}
//tokenAcc is the user account key used to search in the group array.
export function searchGroupUserAcc(tokenAcc){
return (dispatch)=>{
...
let targetG={};
//use tokenAcc to search in groups. every group in groups contains the useraccount info
//expect targetG to hold the return value.
search(tokenAcc, groups,targetG);
console.log(targetG);//still prints{}, but why
};
}
【问题讨论】:
标签: javascript redux pass-by-reference