View foreach result out of it

Asked

Viewed 77 times

-2

Is there any form of data forEach be viewed outside the forEach?

I need to see the data from forEach out of it, but every time I do it it only returns the last data, not all data. I have tried using the localStorage and the same result.

If not, there is some way to make a forEach with two arrays? Could you give examples?

My code:

this.MetaService.CheckOrderGet().subscribe(
              data => {
                const pesquisa = (data as any)
                this.check = JSON.parse(pesquisa._body)            


                this.check.forEach(apielement => {

                  this.fil.forEach(dados=> {            


                    if (apielement.FI == dados.FI) {
                      console.log(`Dados iguais ${apielement.FI}`);                      
                    }else{
                      console.log(`Diferentes ${apielement.FI}`);

                    }

                  })                
              }
            )
  • 2

    can not for a practical example to help understand? o forEach nothing else but check the elements of an array, you can inspect that array at any time, in other parts of the code. "le only returns the last data, not all data", yes, to each interaction an element is accessed, when it ends only has reference to the last, unless you store several elements... "has some way of making a foreach with two arrays?" no, the foreach is for one object (array, Collection) at a time, but you can do one inside the other if you want

  • I did one inside the other, but then I needed to compare the result of one with the other, and in that the results ended up being repeated, then the comparison did not have the correct result. I’ll put my code in to help

  • @Maria here does not scroll to use one for and navigate the other array by Indice no? has any chance of array A being other than B?

  • something else if vc for array[Indice][subindice]. obj vc navigate by it out of for each

  • is that I need a specific data inside them, to be able to compare one with the other. I need to go through the array to access only one data of it @Willian

  • Practically always the arrays will be different and will also have equal always

Show 1 more comment

2 answers

1

I didn’t understand your doubt very well, but this function here makes the comparison between the two arrays, line by line, verifying the data in each position whether they are equal or different. Start by scanning the first array and traverse the second array by comparing the indices.

const a = [1, 2, 3, 4, 5, 6, 7];
const b = [5, 4, 3, 1, 2, 5, 6];

a.forEach(function(valor){
    b.forEach(function(v) {
        if(v === valor)
            console.log('iguais');
        else
            console.log('diferentes');
    });
});
  • Isn’t there any way he could just show us the different and the same? Because it shows the different in the same, and only in the end it shows which are really equal and different

0

this.MetaService.CheckOrderGet().subscribe(
data => {
const pesquisa = (data as any)
this.check = JSON.parse(pesquisa._body)            

this.new = [];
for (var key in this.check) {
  if (this.check[key].FI == this.fil[key].FI){
   this.new[] = `Dados iguais ${this.check[key].FI}`;
  }else{
  this.new[] = `Diferentes ${this.check[key].FI}`;
  }
}

);

the new will be out of forech

  • axo que tem em ema6 or 7 o foreach de key value, transcreve ae se precisa

  • You’re making a mistake in this.new[] = that gets inside the if

  • Dados iguais ${this.check[key].FI};

  • With this.check gives error, I had to put Dados iguais ${apielement.FI};

  • the code went like this this.check.forEach(apielement => {

 this.fil.forEach(dados => {


 for (var key in apielement) {

 console.log(this.new);
 
 if (apielement[key].FI == dados[key].FI) {
 this.new = Equal data ${apielement.FI};
 } else {
 this.new = Different ${apielement.FI};
 }

 }

  • I edited the answer and it would be so for what described

  • that inside the forech? it’s showing only the equal, has how it shows the different?

  • So what else is the structure coming from the backend? pq looking at Aki seems that the code is separating, passes me the data from the backend or one analyzed with a console.log(this.check[key].FI, this.fil[key].FI); below the for and before the if

  • give me the back end array of the 2 array that tests here

  • this.fil data comes from an api, and this.check data comes from another api. pq when this.check data does not have in this.fil, it should be inserted there, and if it is already, it does nothing

  • array of this.check =[
 {
 "FI": "24",
 "porc": "92,509%"
 },
 {
 "FI": "36",
 "porc": "51,445%"
 }
 ] array do this.fil = (8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {FI: "15", porc: "167,701%"}
1: {FI: "02", porc: "138,763%"}
2: {FI: "33", porc: "110,720%"}
3: {FI: "30", porc: "103,536%"}
4: {FI: "24", porc: "100,733%"}
5: {FI: "04", porc: "96,082%"}
6: {FI: "03", porc: "84,180%"}
7: {FI: "20", porc: "75,897%"}

  • take a look at the good https://ideone.com/L1dnQN

  • how n? equal: 24,different: 39,equal: 36, ae ve se and that equal: 24 , 24,different: 39 , 37,equal: 36 , 36... olha la dinovo o ideone

  • in the code here was like this: var neww = [];
 for (var key in this.check) {
 if (this.check[key].FI == this.fil[key].FI) {
 neww.push('igual: ' + this.check[key].FI);
 } else {
 neww.push('diferente: ' + this.check[key].FI);
 }
 }
 
 console.log(neww); right? but then it shows on the console only the different, that in vdd the data q ta showing is not correct, are the same data

  • ah vei;;; vc plays troll wow ne... of course the console.log only ta no different... put one in the same to see

  • I ended up showing the code with a test I was doing, the correct is var neww = [];
 for (var key in this.check) {
 if (this.check[key].FI == this.fil[key].FI) {
 neww.push('igual: ' + this.check[key].FI); 
 } else {
 neww.push('diferente: ' + this.check[key].FI);
 
 }
 } 
 console.log(neww);

  • did you get @Maria? following this function I did it checks the array by position if the array is not equal, and better use @Jrd’s, because it will loop at each position of the array 2

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.