问题
I have an Array and One String Value. I want to get the Hierarchy of the String into an Array.
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.
Here is my Sample Array:
{
"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"
}
]
}
]
}
]
}
And I have the value
let myCategory = "Casuals";
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.
回答1:
Please try below code for your problem. Let me know If you are facing any issue. Please see working demo.
Call getFilterdObject(data,'Polos')
, data
is your object.
function getFilterdObject(obj,param){
let finalArray =[];
finalArray.push(obj.name);
if(obj['name'] != param && obj['children_data']){
let filterData = obj['children_data'].filter(function search(a) {
var children;
if (a.name === param) {
return true;
}
if (!Array.isArray(a.children_data)) {
return false;
}
children = a.children_data.filter(search);
if (children.length) {
a.children_data = children;
return true;
}
});
if(filterData.length){
getArray(filterData, param);
}
else{
finalArray =[];
}
}
function getArray(obj,param){
if(obj.length){
obj.map((val)=>{
finalArray.push(val.name);
if(val.children_data && val.name != param){
getArray(val.children_data, param);
}
});
}
}
return finalArray;
};
回答2:
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);
An example:
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);
来源:https://stackoverflow.com/questions/60089217/i-want-to-get-hierarchy-from-an-array-angular-8