The value in the Operand

var arr = [
    {
        name:"",
        id:1,
        parent_id: 0,
        child:[
        {
          name:"",
          id:2,
          parent_id: 1,
            child:[
              {
                now: true,
                  name:"",
                  id:4,
                  parent_id: 2
              },{
                now: false,
                  name:"",
                  id:5,
                  parent_id: 2
              }
          ]
        },
        {
          name:"",
          id:3,
          parent_id: 1,
          child:[
            {
              now: true,
              name:"",
              parent_id: 3,
              id:6
            },{
              now: false,
              name:"",
              parent_id: 3,
              id:7
            }
          ]
        }]
      },
      {
        name:"",
        id:8,
        parent_id: 0,
        child:[
        {
          name:"",
          id:9,
          parent_id: 8,
            child:[
              {
                now: true,
                  name: "",
                  id:11,
                  parent_id: 9
              },{
                now: false,
                  name:"",
                  id:12,
                  parent_id: 9
              }
          ]
        },
        {
          name:"",
          id:10,
          parent_id: 8,
          child:[
            {
              now: false,
              name:"",
              parent_id: 10,
              id:13
            },{
              now: false,
              name:"",
              parent_id: 10,
              id:14
            }
          ]
        }]
      },
    ];

I want to get the last level of the array if now is true, then find his parent according to his parent_id,
then get it and its parent"s id, so keep getting its parent
and finally return an array result is
so how to find it?

Mar.16,2021

do you mean that there will be multiple arrays like [1BI 2.11] that navigate to the final now=true?
if this is the case, you should start the query from the top. When there are branches at the first level of the query, copy an array corresponding to different branches to the bottom, and then rule out those that do not meet the requirements. This may be more efficient. Of course, this is done on the premise that the data provided is structured.

there are only three correct paths, which are [[1dje 2je 4], [1rect 3je 6], [8pm 9je 11]

.
//
function objP(bArr, inArr){
    var rt=[];
    for(var i=0;i<inArr.length;iPP){
        if(inArr[i].now===false){
            continue;
        } else if(inArr[i].now){
            var newBArr=bArr.concat(inArr[i].id);
            rt.push(newBArr);
            continue;
        } else if(inArr[i].child){
            var tmp=objP([inArr[i].id], inArr[i].child)
            for(var j=0;j<tmp.length;jPP){
                rt.push(bArr.concat(tmp[j]));
            }
        } 
    }
    return rt;
}   

console.log(objP([],arr));  

your idea is clear. Learn the basic syntax and array operations of js and you will know how to do it.

Menu