Sfida: Selezionare Dati Specifici Utilizzando filter()
Compito
Dato un array di oggetti studente, utilizzare il metodo filter() per creare un nuovo array chiamato highScorers che includa solo gli studenti con punteggi d'esame maggiori o uguali a 90.
- L'array originale è fornito come
students, contenente oggetti che rappresentano studenti con le proprietànameescore. - Verificare se il punteggio dello studente è maggiore o uguale a 90.
- Utilizzare il metodo
filter()sull'arraystudentsper creare un nuovo array,highScorers, che includa solo gli studenti con punteggi alti.
123456789101112131415const students = [ { name: "Alice", score: 92 }, { name: "Bob", score: 87 }, { name: "Charlie", score: 95 }, { name: "David", score: 78 }, { name: "Emma", score: 90 }, ]; const highScorers = students.___((___) => { return ___; }); for (let i = 0; i < highScorers.length; i += 1) { console.log(highScorers[i].name); }
Output atteso:
Alice
Charlie
Emma
Il metodo filter() crea un nuovo array includendo solo gli elementi che soddisfano la condizione specificata nella funzione di callback.
123456789101112131415const students = [ { name: "Alice", score: 92 }, { name: "Bob", score: 87 }, { name: "Charlie", score: 95 }, { name: "David", score: 78 }, { name: "Emma", score: 90 }, ]; const highScorers = students.filter((student) => { return student.score >= 90; }); for (let i = 0; i < highScorers.length; i += 1) { console.log(highScorers[i].name); }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you explain how the filter() method works in this example?
What would happen if I changed the score threshold to 85?
Can you show how to display both the name and score of the high scorers?
Awesome!
Completion rate improved to 2.27
Sfida: Selezionare Dati Specifici Utilizzando filter()
Scorri per mostrare il menu
Compito
Dato un array di oggetti studente, utilizzare il metodo filter() per creare un nuovo array chiamato highScorers che includa solo gli studenti con punteggi d'esame maggiori o uguali a 90.
- L'array originale è fornito come
students, contenente oggetti che rappresentano studenti con le proprietànameescore. - Verificare se il punteggio dello studente è maggiore o uguale a 90.
- Utilizzare il metodo
filter()sull'arraystudentsper creare un nuovo array,highScorers, che includa solo gli studenti con punteggi alti.
123456789101112131415const students = [ { name: "Alice", score: 92 }, { name: "Bob", score: 87 }, { name: "Charlie", score: 95 }, { name: "David", score: 78 }, { name: "Emma", score: 90 }, ]; const highScorers = students.___((___) => { return ___; }); for (let i = 0; i < highScorers.length; i += 1) { console.log(highScorers[i].name); }
Output atteso:
Alice
Charlie
Emma
Il metodo filter() crea un nuovo array includendo solo gli elementi che soddisfano la condizione specificata nella funzione di callback.
123456789101112131415const students = [ { name: "Alice", score: 92 }, { name: "Bob", score: 87 }, { name: "Charlie", score: 95 }, { name: "David", score: 78 }, { name: "Emma", score: 90 }, ]; const highScorers = students.filter((student) => { return student.score >= 90; }); for (let i = 0; i < highScorers.length; i += 1) { console.log(highScorers[i].name); }
Grazie per i tuoi commenti!