Skip to content
Advertisement

Manipulating array and finding path

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       } 
  ] 
  1. if All the elements are checked then resultArray will be
    as parentId === null, resultArray = [2] //

  2. if for any element checked flag is true and parentChecked flag is also true, then I need to comapare parentId of that element with objectId of elements. till parentId become null.
    For example in above data, for "Rose" checked is true and parentChecked is also true, then in this condition need to compare it’s parentId with the element having same objectId which is element with id: 2.
    Need to do this till parentId becomes null. it’s parentId is same as ObjectId of Jamie then In this case resultArray will contain resultArray = [98]

  3. Now In case if for each and every element checked flag is not true, than in my result array I want to have id of all elements for which checked flag is true and parentChecked flag is not true.
    In this condition element with parentId null will 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:

https://stackblitz.com/edit/js-mwzndk?file=index.js

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement