skip to Main Content

I have this array nested in another array. I need the processId within the processes array.

I have this and it is not doing the job. I am missing some operator. Variables processIds and processes have the same data.

    "statusIds" :[
            ],
     "menuItems": [
            {
                "menuId": 64,
                "processes": [
                    {
                        "processId": 117,
                        "otherids": [
                            8,
                            3
                        ],
                        "other": []
                    }
                ],
               
                "forms": []
            },
            {
                "menuId": 605,
                "processes": [],
                "forms": []
            }
            ],

    for (let x = 0; x < menuItems.length; x++) {
        let p=menuItems[x].processes
        processIds.push(...p);
     
        
    }
    console.log(processIds)  
    for (let y = 0; y < processIds.length; y++) {
        let p1=processIds[y]
        processes.push(p1)
        
    }   
    console.log(processes)

2

Answers


  1. If you have the menuItems array assigned to the variable menuItems and you’re just trying to pull out the process ids:

    const processIds = menuItems.flatMap(
      menuItem => menuItem.processes.map(
        process => process.processId
      )
    );
    
    Login or Signup to reply.
  2. Changed to line : let p1 = processIds[y].processId

    const menuItems = [{
        "menuId": 64,
        "processes": [{
          "processId": 117,
          "otherids": [
            8,
            3
          ],
          "other": []
        }],
        "forms": []
      },
      {
        "menuId": 605,
        "processes": [],
        "forms": []
      }
    ];
    
    let processIds = [];
    let processes = [];
    for (let x = 0; x < menuItems.length; x++) {
      let p = menuItems[x].processes;
      processIds.push(...p);
    }
    console.log("processIds : ", processIds)
    for (let y = 0; y < processIds.length; y++) {
      let p1 = processIds[y].processId; // added .processId
      processes.push(p1)
    
    }
    console.log("processes : ", processes)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search