我想从数组中获取层次结构-Angular 8 [英] I want to get Hierarchy from an Array - Angular 8

查看:47
本文介绍了我想从数组中获取层次结构-Angular 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组和一个字符串值。我想将字符串的层次结构放入数组中。

I have an Array and One String Value. I want to get the Hierarchy of the String into an Array.

例如,我有一个字符串值 Casuals。 休闲值位于衬衫对象内。 衬衫值位于人对象内。并且男人值位于默认类别对象内。因此,这就是逻辑的工作方式。

For example, I have a string value "Casuals". "Casuals" value is inside the "Shirts" object. "Shirts" value is inside the "Men" object. And "Men" value is inside the "Default Category" object. So, this is how the logic should be work.

这是我的示例数组:

{
  "id": 2,
  "name": "Default Category",
  "children_data": [
    {
      "id": 3,
      "name": "Men",
      "children_data": [
        {
          "id": 11,
          "name": "T-Shirts",
          "children_data": [
            {
              "id": 27,
              "name": "Polos"
            },
            {
              "id": 28,
              "name": "Tees"
            }
          ]
        },
        {
          "id": 12,
          "name": "Shirts",
          "children_data": [
            {
              "id": 30,
              "name": "Casuals"
            },
            {
              "id": 31,
              "name": "Formals"
            }
          ]
        }
      ]
    },
    {
      "id": 4,
      "name": "Women",
      "children_data": [
        {
          "id": 80,
          "name": "Western wears",
          "children_data": [
            {
              "id": 81,
              "name": "T-Shirts"
            },
            {
              "id": 82,
              "name": "Tank & Crop Tops"
            }
          ]
        },
        {
          "id": 21,
          "name": "Ethnic wears",
          "children_data": [
            {
              "id": 51,
              "name": "Kurta & Kurtis"
            },
            {
              "id": 52,
              "name": "Kurta Sets"
            }
          ]
       }
      ]
    }
  ]
}

我的值是


let myCategory = 休闲;

因此,我想获得的最终值是 [默认类别,男装,衬衫, 休闲装 ]

So, that I want to get my final value is ["Default Category", "Men", "Shirts", "Casuals"]

我仍在努力获取值的层次结构。

I'm still struggling to get the Hierarchy of the value.

推荐答案

有必要使用深度优先搜索算法递归搜索更高的对象,然后使用递归方法查找所有父对象:

It is necessary to use Depth First Search Algorithm to recursively search a higher object and then use recursive approach to find all parents:

// Depth First Search Algorithm
function getParentNodeByChild(obj, nameToFind) {
    if (obj.children_data) {
       if (obj.children_data.some(ch => ch.name == nameToFind))
           return obj;
       else {
           for (let item of obj.children_data) {
               if (item.children_data) {
                   let check = this.getParentNodeByChild(item, nameToFind)
                   if (check) {
                       return check;
                   }
               }
           }
       }
    }
    return null
}


function getParentObject(nameToFind) {
    let parentObj;
    if (obj.children_data && obj.children_data.some(ch => ch.name == nameToFind))
        return obj;
    else {
        for (let i = 0; i < obj.children_data.length; ++i) {
            parentObj = getParentNodeByChild(obj.children_data[i], nameToFind);
            if (parentObj)
                break;
        }
        return parentObj;
    }
 }

const getAllNames = keyName => {
    const parentObject = getParentObject(keyName);
    if (parentObject != null && parentObject.name != null) {
        names.push(parentObject.name)
        getAllNames(parentObject.name);
    }
}

let names = [];
let keyToFind = 'Casuals';
getAllNames(keyToFind);
names.push(keyToFind);
console.log(`names`, names);

一个例子:

let obj = {
  "id": 2,
  "name": "Default Category",
  "children_data": [
{
  "id": 3,
  "name": "Men",
  "children_data": [
    {
      "id": 11,
      "name": "T-Shirts",
      "children_data": [
        {
          "id": 27,
          "name": "Polos"
        },
        {
          "id": 28,
          "name": "Tees"
        }
      ]
    },
    {
      "id": 12,
      "name": "Shirts",
      "children_data": [
        {
          "id": 30,
          "name": "Casuals"
        },
        {
          "id": 31,
          "name": "Formals"
        }
      ]
    }
  ]
},
{
  "id": 4,
  "name": "Women",
  "children_data": [
    {
      "id": 80,
      "name": "Western wears",
      "children_data": [
        {
          "id": 81,
          "name": "T-Shirts"
        },
        {
          "id": 82,
          "name": "Tank & Crop Tops"
        }
      ]
    },
    {
      "id": 21,
      "name": "Ethnic wears",
      "children_data": [
        {
          "id": 51,
          "name": "Kurta & Kurtis"
        },
        {
          "id": 52,
          "name": "Kurta Sets"
        }
      ]
   }
  ]
}
  ]
};


// Depth First Search Algorithm
function getParentNodeByChild(obj, nameToFind) {
  if (obj.children_data) {
  if (obj.children_data.some(ch => ch.name == nameToFind))
      return obj;
  else {
      for (let item of obj.children_data) {
          if (item.children_data) {
              let check = this.getParentNodeByChild(item, nameToFind)
              if (check) {
                  return check;
              }
          }
      }
  }
  }
  return null
}


function getParentObject(nameToFind) {
  let parentObj;
  if (obj.children_data && obj.children_data.some(ch => ch.name == nameToFind))
return obj;
  else {
  for (let i = 0; i < obj.children_data.length; ++i) {
      parentObj = getParentNodeByChild(obj.children_data[i], nameToFind);
      if (parentObj)
          break;
  }
  return parentObj;
  }
}


const getAllNames = keyName => {
const parentObject = getParentObject(keyName);
if (parentObject != null && parentObject.name != null) {
  names.push(parentObject.name)
  getAllNames(parentObject.name);
}
}

let names = [];
let keyToFind = 'Casuals';
getAllNames(keyToFind);
names.push(keyToFind);
console.log(`names`, names);

这篇关于我想从数组中获取层次结构-Angular 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆