I am working on a angular application. My data is as follows :
const data =
[ { id: 1212, name: 'julie', checked: true, parentId: 1, parentChecked: false }
, { id: 98, name: 'Rose', checked: true, parentId: 10, parentChecked: true }
, { id: 2, name: 'Jamie', checked: true, parentId: null, parentChecked: false, objectId: 10 }
, { id: 67, name: 'Kosy', checked: false, parentId: 200, parentChecked: undefined }
]
if All the elements are checked then resultArray will be
asparentId === null,resultArray = [2]//if for any element
checkedflag istrueandparentCheckedflag is alsotrue, then I need to comapareparentIdof that element withobjectIdof elements. tillparentIdbecomenull.
For example in above data, for"Rose"checkedistrueandparentCheckedis alsotrue, then in this condition need to compare it’s parentId with the element having same objectId which is element withid: 2.
Need to do this tillparentIdbecomes null. it’sparentIdis same asObjectIdofJamiethen In this case resultArray will containresultArray = [98]Now In case if for each and every element
checkedflag isnot true, than in my result array I want to have id of all elements for whichcheckedflag istrueandparentCheckedflag isnot true.
In this condition element withparentIdnullwill not be considered as we have take it in above mentioned scenario.
For example, in data above, for some element’s I have “checked” flag as true and "parentChecked" flag is not true and for some “checked” is false, in this case, result array will look as follows:
resultArray = [1212,67] // as for id = 1212 and id =67 checked is true and parentChecked flag is false or undefined // But in this condition we wont take element with parenId null in // consideration as for each and every element "checked" flag is not true.
Once I get my resultArray, I want to pass each id of resultArray in findArray method in my stackblitz
https://stackblitz.com/edit/js-8usqcc?file=index.js
Advertisement
Answer
Is this what you mean?
interface dataObj {
id: number,
name: string,
checked: boolean,
objectId ? : number,
parentId ? : number,
parentChecked: boolean
}
const data = [{
id: 1212,
name: 'julie',
checked: true,
parentId: 1,
parentChecked: false
}, {
id: 98,
name: 'Rose',
checked: true,
parentId: 10,
parentChecked: true
}, {
id: 2,
name: 'Jamie',
checked: true,
parentId: 200,
parentChecked: true,
objectId: 10
}, {
id: 20,
name: 'JamieParent',
checked: true,
parentId: null,
parentChecked: false,
objectId: 200
}, {
id: 67,
name: 'Kosy',
checked: false,
parentId: 200,
parentChecked: undefined
}]
let resultArray_allTrueWithParentIdNull = []
let resultArray_someTrueWithParentIdNotNull = []
let resultArray_isCheckedAndParentChecked = []
let allTrue = true
// this function is SOLEY for condition 2
const recursiveCheckForParentValues = (el: dataObj, array: number[]) => {
// first add this id to the array since it meets our condition 2
array.push(el.id)
// now find it's parent if there is one
// it wil only find the parent
let parent = data.filter((inel: dataObj) => inel.objectId == el.parentId)
if (parent.length > 0) {
parent = parent[0];
// we found the parent, now test it for condition 2
if (parent.checked && parent.parentChecked && parent.parentId) array.push(parent.id);
// if our parent is ALSO a parent (has parentId), run it through this function again, and as many times as it takes until we hit NULL
// we are sending array through as an argument, so it will keep accumulating IDs along the way
if (parent.parentId) return recursiveCheckForParentValues(parent, array)
}
// return the final array
return [...new Set(array)]; // removes duplicates
}
// loop through the array
data.forEach((el: dataObj) => {
if (!el.checked) allTrue = false;
else {
if (!el.parentId) resultArray_allTrueWithParentIdNull.push(el.id);
else if (el.parentChecked && el.checked) resultArray_isCheckedAndParentChecked = recursiveCheckForParentValues(el, resultArray_isCheckedAndParentChecked)
else resultArray_someTrueWithParentIdNotNull.push(el.id)
}
})
console.log(resultArray_allTrueWithParentIdNull);
console.log(resultArray_someTrueWithParentIdNotNull)
console.log(resultArray_isCheckedAndParentChecked)Then to pass these values on to your findInArray method, you could do something like this
var output = { paths: [], found: false };
resultArray.forEach(id => findInArray(data, id, output));
console.log(output.found);
I also modified your stackblitz to accept an array of ids. Let me know if this works foryou: