How to Filter an array using the For structure?

Asked

Viewed 73 times

1

how I can filter an array using the structure for ?

The code is this:

const numeros = [1,2,3,4,5,55,190,355,747,1000,125];

I need to filter the numbers under 10.

I used it this way, but I was asked to create one using the structure for and I can’t get out of 0.

const numeros = [1,2,3,4,5,55,190,355,747,1000,125];

const filterOne = x => x < 10;

const filterTwo = numeros.filter(filterOne);

console.log(filterTwo);

  • @Valdeirpsr .

2 answers

3


Just make a condition, it can also be done so:

numeros.forEach((n) => {
  if (n < 10) {
    filterTwo.push(n);
  }
});

const numeros = [1,2,3,4,5,55,190,355,747,1000,125];

const filterOne = x => x < 10;
const filterTwo = [];

for (let i = 0; i < numeros.length; i++) {
  // Verifica que o valor de "numeros" no índice "i" é menor que 10
  if (numeros[i] < 10) {
    // adiciona no array filterTwo 
    filterTwo.push(numeros[i]);
  }
}

console.log(filterTwo);

Reference:

  • That’s great, in case it was words instead of numbers. ?

  • Yes, but the condition would be: if (nomes[i] === 'Maria'), if the value to be checked was in a variable if (nomes[i] === nome).

  • Our dear, you are awesome. Now it’s clear, and I understand how to do it. Mt thank you God bless

  • I’m nothing but an apprentice.

  • One last question, in case I wanted to filter it through the letters. Example: const names = ["jean", "souza", "wm", "Marcelo"]; ?

  • Thank you, quick kk. What does it mean to say this code: numbers[i] That the number elements are in i ?

  • [1,2,3,4,5,55,190,355,747,1000,125] .

Show 3 more comments

2

You can use another loop shape, forEach, where the parameter (e) represents the value of each item in the array:

const numeros = [1,2,3,4,5,55,190,355,747,1000,125];

const result = [];
numeros.forEach((e)=>{
   e < 10 && result.push(e);
});

console.log(result);

Explanation of Short Circuit Operators used in the above example:

Returns the second operand based on the value of the first one. If the first one is false, the second operand is ignored.

e < 10 && result.push(e);
\____/  ↑ \____________/
 1º op. |     2º op.
        |
O 1º op. tem que ser true

e < 10 || result.push(e);
\____/  ↑ \____________/
 1º op. |     2º op.
        |
O 1º op. tem que ser false

Already the Operadores Ternários (suit, of 3) are formed by 3 operands, separated by ? (meets the 1st op. ) and : (does not meet the 1st op. , similar to the else):

e < 10 ? faz uma coisa : faz outra coisa;
  • What is the name given for this verification: e < 10 && result.push(e);? First time I see it, I know I can do it like this: (e < 10): 'sim' ? 'não';.. but I also don’t remember the name.

  • 1

    That’s the short Circuit Operators: returns the value of the second operand based on the result of the first. Ternary operator is when there are 3 operators, as in your example above.

  • Seeing the answers to the question How does this if/Else work with "?" and ":"? and the documentation: Operators Lógicos, became clearer the operation.

  • dvd the link Short Circuit Operators broke.

Browser other questions tagged

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