Knowing which index of array values gives "match" with variable

Asked

Viewed 39 times

2

I have two JSON arrays in Javascript and both are like this.

First array:

[""Amarelo"", ""Amarelo"", ""Amarelo"", ""Preto"", ""Preto"", ""Preto""]

Second array:

[""Pequeno"", ""Médio"", ""Grande"", ""Pequeno"", ""Médio"", ""Grande""]

And I can have two variables that change by clicking that can be, for example "yellow" and "medium".

The question is: how do I know the index of when the values of the variables match those of the arrays?

Looking like this I know that the index that gives "match" is 1, but how do I make Javascript discover it by itself?

At this discovered index value I will be able to call the rest of the content dynamically.

Can anyone suggest a north for me?

  • you could do a "for" to go through the arrays.

  • Yes, I imagine that this is the way, but I had no idea what the logic would be for such or how to structure this action. Would you do a for each array? Or would you pick that up at a while? I haven’t had the balcony yet. :(

  • The two arrays always have the same length?

  • Yes, they both have the same length.

4 answers

2

If the two arrays are always the same size, loop a counter based on one of them, and inside check the value of the two. For example:

var cores = ["Amarelo", "Amarelo", "Amarelo", "Preto", "Preto", "Preto"];
var tamanhos = ["Pequeno", "Médio", "Grande", "Pequeno", "Médio", "Grande"];

var corEsperada = 'Amarelo';
var tamanhoEsperado = 'Médio';

for(var i=0; i<cores.length; i++) {
    if(cores[i] === corEsperada && tamanhos[i] === tamanhoEsperado) {
        alert('Encontrado o par ' + corEsperada + '/' + tamanhoEsperado + ' na posição ' + i);
    }
}

  • Thanks bfavaretto, in the end it was just that! Thanks for the idea. Hugs

0

Proposal:

var cores = [ 'amarelo', 'amarelo', 'preto', 'azul' ];
var corProcurada = 'preto';
var indice;

alert((indice = cores.indexOf(corProcurada)) > -1 ? indice + 1 : 'nao achei'); // 3

Source: here

0

You can use the function indexOf(). In your case it would look like this:

var array1 = ["Amarelo", "Amarelo", "Amarelo", "Preto", "Preto", "Preto"];
var variavel1 = "Amarelo";
var index1 = array1.indexOf(variavel1);

index1 will be the value of the first element equal to "Yellow", 1 in the case. If it does not find, returns -1.

0

Another solution in ES6 would be to use the method .some(). So you interrupt the loop as soon as the condition is satisfied:

The some() method tests whether some of the elements in the array pass the test implemented by the assigned function.

const cores = ['Amarelo', 'Amarelo', 'Amarelo', 'Preto', 'Preto', 'Preto']
const tamanhos = ['Pequeno', 'Médio', 'Grande', 'Pequeno', 'Médio', 'Grande']

const corUsuario = (corUsr) => corUsr === 'Preto'
const tamUsuario = (tamUsr) => tamUsr === 'Grande'

cores.some((cor, i) => {
  if (corUsuario(cor) && tamUsuario(tamanhos[i])) {
    console.log(`produto disponivel: cor ${cor}, tamanho ${tamanhos[i]} no índice ${i}`)
  }
})

Browser other questions tagged

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