Javascript使用嵌套数组中的find

时间:2018-09-04 18:43:37

标签: javascript arrays

我有以下数组数据

var data = [
    {name:'hr', to:'/hr/settings', children:[
        {name:'Language', to:'/hr/settings/general/languages', icon:''},
        {name:'Marital Status', to:'/hr/settings/general/marital-status', icon:''},
        {name:'Nationalities', to:'/hr/settings/general/nationalities', icon:''},
        {name:'Ethnicities', to:'/hr/settings/general/ethnicities', icon:''},
        {name:'Religions', to:'/hr/settings/general/religions', icon:''},
        {name:'Tribes', to:'/hr/settings/general/tribes', icon:''},
        {name:'Relations', to:'/hr/settings/general/relations', icon:''}
    ]},
    {name:'education', to:'/hr/education', children:[
        {name:'Universities',to:'/hr/settings/education/universities', icon:''},
        {name:'Relations',to:'//hr/settings/education/relations', icon:''}
    ]}
];

所以期待做的是找到数据数组的索引,该索引必须在数组或其子元素中赋值,类似于某个字符串

所以我有以下内容

function getArrayIndex(tovalue) {
    const pathnavigated = data.find(link => link.to === tovalue);
    return data.indexOf(pathnavigated);
}

上面的方法适用于数组getArrayIndex('/hr/settings'),但现在我希望它也搜索子级

例如

getArrayIndex('/hr/settings/general/marital-status') 

应返回第一个索引

getArrayIndex('/hr/settings/education/universities')应该返回第二个索引

我如何使该函数搜索子项,并返回(子项)父数组上的索引值

3 个答案:

答案 0 :(得分:2)

我只是对您的函数getArrayIndex做了一些修改,并添加了以下内容:

data.find(link => link.children.filter(b=>b.to == tovalue).length > 0)

在这里,我过滤子项,如果至少有一个与您的字符串匹配的子项,则将其返回。

var data = [
      {name:'hr',to:'/hr/settings', children:[
                    {name:'Language',to:'/hr/settings/general/languages', icon:''},
                    {name:'Marital Status',to:'/hr/settings/general/marital-status', icon:''},
                    {name:'Nationalities',to:'/hr/settings/general/nationalities', icon:''},
                    {name:'Ethnicities',to:'/hr/settings/general/ethnicities', icon:''},
                    {name:'Religions',to:'/hr/settings/general/religions', icon:''},
                    {name:'Tribes',to:'/hr/settings/general/tribes', icon:''},
                    {name:'Relations',to:'/hr/settings/general/relations', icon:''}]},   
       {name:'education', to:'/hr/education', children:[
                    {name:'Universities',to:'/hr/settings/education/universities', icon:''},
                    {name:'Relations',to:'//hr/settings/education/relations', icon:''}]}

  ]
  
 function getArrayIndex(tovalue){
    const pathnavigated = data.find(link => link.children.filter(b=>b.to == tovalue).length > 0);
    return data.indexOf(pathnavigated);
}

console.log(getArrayIndex('/hr/settings/education/universities') )

答案 1 :(得分:2)

我将使用some()而不是filter(),因此您不必检查长度。如果任何一个子代通过了lambda中的支票,则某些返回true。如果您仍想搜索顶级商品,请不要忘记检查以下两个条件:link.to === tovalue || link.children.some(c => c.to === tovalue)

function getArrayIndex(tovalue) {
    const pathnavigated = data.find(link =>
        link.to === tovalue || link.children.some(c => c.to === tovalue));
    return data.indexOf(pathnavigated);
}

答案 2 :(得分:1)

有人已经击败了我..但仍然..

function getIndex(array, search) {
    for (let i = 0;i < array.length; i++) {
        if (array[i].to === search) {
            return i;
        }

        if (Array.isArray(array[i].children)) {
            for (let j = 0; j < array[i].children.length; j++) {
                if (array[i].children[j].to === search) {
                    return j;
                }
            }
        }
    }

    return -1;
}