递归JavaScript函数,用于通过UID定位对象

时间:2018-06-13 21:02:45

标签: javascript recursion

我在返回已在此分层树中找到的元素时遇到问题。 例如,如果我选择的项目是:

/

我会将import Control.Arrow import Data.Maybe type Partial = Kleisli Maybe isDefinedAt :: Partial a b -> a -> Bool isDefinedAt f x = isJust $ runKleisli f x -- laziness should save some of the work, if possible orElse :: Partial a b -> Partial a b -> Partial a b orElse = (<+>) andThen :: Partial a b -> Partial b c -> Partial a c andThen = (>>>) 与下面的对象数组匹配。

&#13;
&#13;
{
 "UID": 49,
 "GUID": "",
 "LocationName": "Doctor Smith's Office",

 "LocationType": {
    "UID": 2,
    "LocationTypeName": "Practice",
    "Description": "other location"
 }
}
&#13;
&#13;
&#13;

&#13;
&#13;
UID
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

使用递归reduce函数要容易得多,如下所示:

&#13;
&#13;
const input={UID:2,GUID:"",LocationName:"USA",ParentLocation:null,subs:[{UID:42,GUID:"",LocationName:"New Jersey",Description:"",subs:[{UID:3,GUID:"",LocationName:"Essex County",ParentLocation:null,"subs":[{UID:4,LocationName:"Newark",ParentLocation:3,"subs":[{"UID":49,"GUID":"","LocationName":"Doctor Smith's Office","LocationType":{"UID":2,"LocationTypeName":"Practice","Description":"other location"},"subs":[{"HostID":38,"HostName":"Ocean Host",}]}]}]}]}]};

const findUIDObj = (uid, parent) => {
  const { UID, subs } = parent;
  if (UID === uid) {
    const { subs, ...rest } = parent;
    return rest;
  }
  if (subs) return subs.reduce((found, child) => found || findUIDObj(uid, child), null);
};
console.log(findUIDObj(49, input))
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用显式功能搜索所需的UID

&#13;
&#13;
function find(array, UID) {
    var object;
    array.some(o => {
        if (o.UID === UID) {
            return object = o;
        }
        return object = find(o.subs, UID);
    });
    return object;
}

var object = { UID: 2, GUID: "", LocationName: "USA", ParentLocation: null, subs: [{ UID: 42, GUID: "", LocationName: "New Jersey", Description: "", subs: [{ UID: 3, GUID: "", LocationName: "Essex County", ParentLocation: null, subs: [{ UID: 4, LocationName: "Newark", ParentLocation: 3, subs: [{ UID: 49, GUID: "", LocationName: "Doctor Smith's Office", LocationType: { UID: 2, LocationTypeName: "Practice", Description: "other location" }, subs: [{ HostID: 38, HostName: "Ocean Host", }] }] }] }] }] };

console.log(find([object], 49));
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 2 :(得分:1)

执行此操作的一种方法是编写一个相当通用的树查找功能版本,然后针对您的特定问题对其进行配置。在这里,我们选择通过匹配提供的UID来测试,我们通过查看 $.ajax({ url : 'RoleServlet', type: 'POST', data : { action:"get" }, success : function(responseText) { $('#role-data-table tbody').empty(); for(x in responseText){ $("#role-data").append("<tr><td>"+responseText[x].role+"</td> </tr>"); } $('#role-data-table').DataTable(); } }); 属性进入子项,我们通过去掉subs属性来转换结果:

subs

但是如果我们不想直接传递const searchTreeDF = (kids, test, convert, node) => test(node) // depth-first search ? convert(node) : (kids(node) || []).reduce( (found, child) => found || searchTreeDF(kids, test, convert, child), false ) const subs = node => node.subs const matchId = (uid) => (item) => item.UID === uid const convert = ({subs, ...rest}) => ({...rest}) const findUid = (uid, tree) => searchTreeDF(subs, matchId(uid), convert, tree) // ... const tree = {"GUID": "", "LocationName": "USA", "ParentLocation": null, "UID": 2, "subs": [{"Description": "", "GUID": "", "LocationName": "New Jersey", "UID": 42, "subs": [{"GUID": "", "LocationName": "Essex County", "ParentLocation": null, "UID": 3, "subs": [{"LocationName": "Newark", "ParentLocation": 3, "UID": 4, "subs": [{"GUID": "", "LocationName": "Doctor Smith's Office", "LocationType": {"Description": "other location", "LocationTypeName": "Practice", "UID": 2}, "UID": 49, "subs": [{"HostID": 38, "HostName": "Ocean Host"}]}]}]}]}]} console.log(findUid(49, tree)),而是想传递一个具有自己的UID属性的元素,我们就可以编写

UID

然后执行此操作:

const matchElem = (elem) => (item) => elem.UID === item.UID

或者,如果我们不想转换结果并保留const findUid2 = (elem, tree) => searchTreeDF(subs, matchElem(elem), convert, tree) // ... findUid2({UID: 49}, tree) 属性,我们可以为subs提供身份函数:

convert

或者我们可以随意混合搭配。另请注意,配置不必使用命名函数。我们可以轻松写出

const findUid = (uid, tree) => searchTreeDF(subs, matchId(uid), x => x, tree)

通用功能并不总是正确的答案。但是他们可以帮助将那些改变的东西从我们正在编写的更基本的算法中分离出来。我认为在这种情况下,它有助于使事情更易于维护。