Skip to content

Instantly share code, notes, and snippets.

@zilahir
Created May 7, 2020 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zilahir/5fce65259e8a0ae9f9fe5537bd2157ff to your computer and use it in GitHub Desktop.
Save zilahir/5fce65259e8a0ae9f9fe5537bd2157ff to your computer and use it in GitHub Desktop.
findInDeepArray
/**
* @param {number} tagId the id of the tags we are looking for it's child
* @param {Array} tags array of the level we are looking for the child tags
* @returns {object} object of the found child object
*/
function findTagById(tagId, tags) {
// console.debug("findTagById > Tags", tags);
const result = filter(tags, { item: [{ tag_id: tagId }] });
// console.debug("findTagById > result", result);
return result[0].item.filter(t => t.tag_id === tagId)[0];
}
/**
* @param {object} tag object of the tag
* @description checks if a tag has child, if so returns them, returns fale otherwise
* @returns {Array} Array of the found child tags
*/
function hasChildTags(tag) {
// console.debug('findingChildTag', tag)
if (
tag.relations &&
tag.relations.relation &&
tag.relations.relation.length > 0
) {
// console.log("has child", tag);
return tag.relations.relation;
}
return false;
}
/**
* @param {number} level the level the function should iteate over
* @param {object} raw the raw structure in JSON format
* @returns {Array} returns the result array of the tree of the tegs
*/
export function getLevel(level, raw) {
const TREE = {
id: 0,
filename: "All",
children: []
};
const thisLevelTags = raw.categories.category[level];
// console.log("thislevelTags", thisLevelTags.item);
const { item } = thisLevelTags;
if (item.length !== 0) {
//console.log("item", item);
item.forEach(element => {
// console.log("thisTag: ", element);
TREE.children.push({
id: element.id,
filename: element.name,
...element
});
});
}
return TREE;
}
let resultTagTree = getLevel(0, raw);
// console.debug(resultTagTree);
function findTagInTree(tagId, tags) {
// console.debug('tags:', tags)
const result = findIndex(tags, ["tag_id", tagId]);
// console.debug(result)
return result;
}
function findTagInDeepTree(tagId, tags) {
// console.debug('tags:', tags)
let result;
const temp = filter(tags, {
children: [{ tag_id: tagId }]
});
if (temp.length) {
result = temp[0].children.find(currTag => currTag.tag_id === tagId);
console.debug(result);
}
return result;
// result[0].children.find(currTag => currTag.tag_id === tagId)
}
function getDeeperTags() {
for (let i = 1; i < raw.categories.category.length; i++) {
const thisLevelTags = raw.categories.category[i];
const { item } = thisLevelTags;
if (item && item.relations) {
//console.debug('item: ', item, 'relations : ', item.relations.relation)
const relations = item.relations.relation;
//console.debug("item", item, "relations", relations);
// console.debug('item', item)
// console.debug('relations', relations)
const parentId = Array.isArray(item.relations.relation)
? item.relations.relation[0]
: item.relations.relation;
//console.debug("parentId", parentId);
const parentIndexOfThis = findTagInDeepTree(
parentId,
resultTagTree.children
);
//console.debug("parantIdOfThis: ", parentId, parentIndexOfThis);
} else {
if (Array.isArray(item)) {
item.forEach(e => {
const parentId = Array.isArray(e.relations.relation)
? e.relations.relation[0]
: e.relations.relation;
// console.debug('parentId', parentId)
// console.debug("thisTag", e);
const parentIndexOfThis = findTagInTree(
parentId,
resultTagTree.children
);
// console.debug("parentOfThis", parentIndexOfThis);
// console.debug( "resultTagList", resultTagTree.children[parentIndexOfThis] );
if (
typeof resultTagTree.children[parentIndexOfThis].children ===
"undefined"
) {
// create children here
resultTagTree.children[parentIndexOfThis].children = [];
resultTagTree.children[parentIndexOfThis].children.push({
id: e.tag_id,
filename: e.tag_name,
...e
});
} else {
resultTagTree.children[parentIndexOfThis].children.push({
id: e.tag_id,
filename: e.tag_name,
...e
});
}
});
}
// console.debug(resultTagTree)
}
}
}
getDeeperTags();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment