Home > Back-end >  Nested loop and too many duplicates
Nested loop and too many duplicates

Time:01-31

I made a nested for loop because I called an API which send me back its data that I compare with my data. And I need to replace part of my data when its value of property is not the same as the API's one, the correct data will be saved in a new array.

dataI would be my data and dataO would be the data from API, which is the correct one.

  let dataI = [{firstName:"John"}, {firstName:"Marc"}, {firstName:"Alex"}];
  let dataO = [{firstName:"John"}, {firstName:"Marc"}, {firstName:"Alexis"}];

I am looking forward to a result like this:

let new_arr = [{dataI[1]}, {dataI[2]}, {dataO[3]}]

which translate into:

let new_arr = [{firstName:"John"}, {firstName:"Marc"}, {firstName:"Alexis"}];

The correction would been made with {dataO[3]}. So here is my code:

  let new_arr = [];
    
          for (i = 0; i < dataI.length; i  ) {
            for (o = 0; o < dataO.length; o  ) {
              
              if (dataI[i].property !== dataO[o].property) {
                
                dataI[i].property = dataO[o].property;
                new_arr.push(dataI[i]);

              }

            }
          }

It is working but in my result in new_arr, I get too many duplicates of the dataI[i].property.

So far I am getting something similar to:

let new_arr = [{firstName:"John"}, {firstName:"John"}, {firstName:"John"}, {firstName:"John"}, {firstName:"John"}, {firstName:"John"}, {firstName:"John"}, {firstName:"John"}, {firstName:"John"}, {firstName:"Marc"}, {and a lot more}]

So I thought about this:

let new_arr = [];
        
              for (i = 1; i < dataI.length; i  ) {
                for (o = 1; o < dataO.length; o  ) {
                  
                  if (dataI[i].property !== dataO[o].property) {
                    
                    dataI[i].property = dataO[o].property;
                    
                    if (dataI[i].property == dataI[i-1].property) {
                    
                      delete dataI[i-1].property; 

                    }

                    new_arr.push(dataI[i]);
 
                  }

                }
              }

Which translate into:

let new_arr = [];
            
                  for (i = 1; i < dataI.length; i  ) {
                    for (o = 1; o < dataO.length; o  ) {
                      
                      if (dataI[i].firstName!== dataO[o].firstName) {
                        
                        dataI[i].firstName= dataO[o].firstName;
                        
                        if (dataI[i].firstName== dataI[i-1].firstName) {
                        
                          delete dataI[i-1].firstName; 
    
                        }
    
                        new_arr.push(dataI[i]);
     
                      }
    
                    }
                  }

I went with for (i = 1; i < dataI.length; i ) instead of (i = 0; i < dataI.length; i ) because it was a bit strange to have i = -1 in my condition if (dataI[0].firstName == dataI[0-1].firstName).

But it doesn't work I get the error "TypeError: Cannot read property 'property' of undefined"

CodePudding user response:

I'm not sure that I have correctly understand your problem, but can this help you ?

let dataI = [{firstName:"John"}, {firstName:"Marc"}, {firstName:"Alex"}];
let dataO = [{firstName:"John"}, {firstName:"Marc"}, {firstName:"Alexis"}];
  
let new_arr = [];

for (i = 0; i < dataI.length; i  ) {
  let existInO = false;
  for (o = 0; o < dataO.length; o  ) {
    if (dataI[i].firstName == dataO[o].firstName) {
      existInO = true;
    }
  }
  existInO ? new_arr.push(dataI[i]) : new_arr.push(dataO[i]);
  
}

console.log(new_arr);
Output:

[
  { firstName: 'John' },
  { firstName: 'Marc' },
  { firstName: 'Alexis' }
]

CodePudding user response:

How about one loop through dataI and compare dataI[i] with dataO[i], also not creating a new array but updating your existing dataI array:

for (i = 0; i < dataI.length; i  ) {
  if (dataO[i] && dataI[i].firstName !== dataO[i].firstName) {
    dataI[i].firstName = dataO[i].firstname;
  }
}
  •  Tags:  
  • Related