3

I am trying to find the first occurrence of id from the nested data and if it is founds want to update which path it took to reach there with the index and move to the next iterations.

For Example:

let error = [
  {
    id: 1,
    message: 'test1',
    key: 'surname',
    type:1,
    path: [],
  },
  {
    id: 1,
    message: 'test12',
    key: 'firstname',
    type:2,
    path: [],
  },
  {
    id: 2,
    message: 'test2',
    key: 'surname',
    type:2,
    path: [],
  },
];

//data

data = {
  txn: [
    {
      actions: [
        {
          source: [
            {
              type: 1,
              id: 1,
              profile: {
                firstname: 'data1',
              },
            },
          ],
          cond: [
            {
              type: 2,
              id: 1,
              profile: {
                firstname: 'data1',
                surname: 'data2',
              },
              onb: [
                {
                  type: 2,
                  id: 2,
                  profile: {
                    firstname: 'data21',
                    surname: 'data22',
                  },
                },
              ],
            },
          ],
        },
      ],
    },
  ],
};

When id=1 is found in the nested data and type 1 is matched at the source of the 0 indexes, I wanted to update the path with the ['txn',0,'actions',0,'source',0] and move to the next error object

when id:1 is found in the nested data and type 2 matched ['txn',0,'actions',0,'cond',0]

when id:2 is found in the nested data and type 2 matched ['txn',0,'actions',0,'cond',0,'onb',1]

so my output would be

error = [
  {
    id: 1,
    message: 'test1',
    key: 'surname',
    type:1,
    path: ['txn',0,'actions',0,'source',0],
  },
  {
    id: 1,
    message: 'test12',
    key: 'firstname',
    type:2,
    path: ['txn',0,'actions',0,'cond',0],
  },
  {
    id: 2,
    message: 'test2',
    key: 'surname',
    type:2,
    path: ['txn',0,'actions',0,'cond',0,'onb',1]
  },
];

I tried the following code but two issues ,I am facing

  1. it is not updating the path
  2. it is not ending the loop properly.

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

function findVal() {
  error.forEach((ev) => {
    let stop = false;
    //console.log(ev)
    //search for occurance of the id with type matching
    //if matched update the value in the error
    //move to the next error once first occurance found
    for (let i = 0; i < data.txn.length; i++) {
      let actiondata = data.txn[i].actions;
      for (let j = 0; j < actiondata.length; j++) {
        let sourcedata = actiondata[j].source;
        for (let k = 0; k < sourcedata.length; k++) {
          if (ev.id === sourcedata[k].id && sourcedata[k].type == ev.type) {
            console.log('matched', i, j, k);
            //matched
            //update error object
            ev.path.push('txn');
            console.log(ev.path);
            ev.path.push(i);
            ev.path.push('actiondata');
            ev.path.push(j);
            ev.path.push('source');
            ev.path.push(k);
            stop = true;
          }
          if (stop) {
            break;
          }
        }

        //cond loop
        let conddata = actiondata[j].cond;
        for (let l = 0; l < conddata.length; l++) {
          if (
            ev.id === conddata[l].id &&
            !stop &&
            conddata[l].type == ev.type
          ) {
            console.log('matched', i, j, l);
            //matched
            //update error object
            ev.path.push('txn');
            ev.path.push(i);
            ev.path.push('actiondata');
            ev.path.push(j);
            ev.path.push('cond');
            ev.path.push(l);
            stop = true;
            //onb loop
            let onbdata = conddata[l].onb;
            for (let m = 0; m < onbdata.length; m++) {
              if (
                ev.id === onbdata[m].id &&
                !stop &&
                onbdata[m].type === ev.type
              ) {
                //matched
                //update error object
                ev.path.push('txn');
                ev.path.push(i);
                ev.path.push('actiondata');
                ev.path.push(j);
                ev.path.push('cond');
                ev.path.push(l);
                ev.path.push('onb');
                ev.path.push(m);
                stop = true;
              }

              if (stop) {
                break;
              }
            }
          }
          if (stop) {
            break;
          }
        }

        if (stop) {
          break;
        }
      }
      if (stop) {
        break;
      }
    }
    console.log(ev);
  });
}

findVal();

Can anyone please help me what I am doing wrong?

Bravo
  • 61
  • 2
  • 7
  • 26
  • Does this answer your question? [What's the best way to break from nested loops in JavaScript?](https://stackoverflow.com/questions/183161/whats-the-best-way-to-break-from-nested-loops-in-javascript) – Konrad Nov 15 '22 at 20:35
  • Thanks, Konard for the response, Actually, I tried the same solution but somehow it is not working and also I am looking for help to update the path before braking the loop. – Bravo Nov 15 '22 at 20:37
  • 1
    I would use the a function with return - extract the loops to a new method and call return, when you want to abort all nested loops. Of course - most probably you have to return data, that you are able to continue there, or know how to access the found node etc. – BitLauncher Nov 15 '22 at 22:15

0 Answers0