Cara menggunakan foreach break javascript

Bagaimana Anda bisa memutuskan dan kembali dari forEach loop di JavaScript? . kamu tidak bisa. Memutus dari forEach loop dalam JavaScript tidak dimungkinkan. Namun, ada beberapa solusi, dan ada juga alternatif

Meskipun kami tidak dapat memutuskan dan mengembalikan dari forEach dalam JavaScript, kami masih dapat mengembalikan nilai dengan variabel eksternal dengan cara berikut

let productDetails

products.forEach(product => {
    if (product.id === 123) {
        productDetails = product 
    }
})
forEach. js Solusi untuk mengembalikan nilai dari forEach

Disalin ke papan klip. Salin

Di sini kami pada dasarnya menetapkan variabel di luar forEach loop ke salah satu item, berdasarkan ID. Mengingat kondisi yang tepat di dalam pernyataan

// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
0, kita dapat mengambil dan "mengembalikan" nilai yang benar. Namun, ini dapat ditulis ulang dengan cara yang lebih baik. Berikut adalah lima alternatif berbeda yang dapat Anda gunakan saat Anda perlu istirahat dari forEach

Ingin meningkatkan keterampilan Anda?

Cara menggunakan foreach break javascript

Menggunakan Array. Temukan

Contoh di atas juga dapat ditulis ulang dengan metode

// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
2. Seperti namanya, metode
// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
2 digunakan untuk menemukan elemen tertentu dalam array. Ini mengembalikan satu item yang pertama kali lulus tes dalam fungsi callback. Sebagai contoh

// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
temukan. js Mengembalikan item yang diberi predikat

Disalin ke papan klip. Salin

Untuk contoh pertama, kami memiliki satu objek yang dikembalikan karena setiap ID unik. Tetapi pada yang kedua, kami mungkin memiliki beberapa produk yang sedang diobral, tetapi kami masih mendapatkan kembali hanya yang pertama yang lolos uji. Jika tidak ada kecocokan, metode

// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
_2 hanya mengembalikan
// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
5


Menggunakan Array. findIndex

Solusi serupa tetapi sedikit berbeda untuk

// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
_2 menggunakan metode
// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
7. Jika Anda perlu mengembalikan indeks elemen di dalam array, Anda dapat menggunakan metode
// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
7. Ini akan mengembalikan indeks elemen jika ditemukan. Jika tidak ada elemen yang ditemukan dengan predikat, itu akan mengembalikan -1

// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
findIndex. js Mengembalikan indeks item, atau -1 jika tidak ada item yang ditemukan

Disalin ke papan klip. Salin

Ingin meningkatkan keterampilan Anda?

Cara menggunakan foreach break javascript

Menggunakan Array. beberapa untuk Mengembalikan Benar atau Salah

Jika Anda perlu keluar dari loop dan mengembalikan boolean, kita dapat menggunakan metode

// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
9. Tidak seperti
// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
_2 di mana kami mengharapkan item dikembalikan dari array,
// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
9 mengembalikan
// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
2 atau
// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
3. Jika setidaknya satu item dalam array cocok dengan kriteria, metode ini akan langsung mengembalikan
// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
2. Ambil yang berikut ini sebagai contoh

// We do have some products which are on sale so we get back true
products.some(product => product.onSale)

// None of the items are less than 0, so we get back false
[1, 2, 3].some(item => item < 0)

_ beberapa. js Mengembalikan benar atau salah jika setidaknya satu item melewati predikat

Disalin ke papan klip. Salin

Pada contoh pertama, kami mendapatkan kembali _______10_______2 karena kami hanya tertarik pada apakah setidaknya ada satu item yang dijual. Demikian pula, dalam contoh kedua, kami hanya tertarik pada apakah setidaknya satu item di bawah 0. Karena tidak ada yang di bawah 0, metode mengembalikan

// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
3


Menggunakan Array. setiap untuk Memeriksa Setiap Elemen

// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
_7 sangat mirip dengan
// This will return the product with the ID of 1
products.find(product => product.id === 1)

// Even though we might have multiple products which are on sale
// this will only return the first match
products.find(product => product.onSale)

// This will return undefined
products.find(product => product.id === 'invalid')
9 dalam hal nilai yang dikembalikan. kembalikan _______10________2 atau _______10_______3. Namun, alih-alih memeriksa apakah satu item lulus predikat, item tersebut hanya akan mengembalikan
// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
2 jika semua item lulus tes. Mari kita lihat sebuah contoh

// This produces false
products.every(product => product.onSale)

// This produces true
products.every(product => product.name)
_ setiap. js Mengembalikan true jika semua item lulus predikat. Jika tidak mengembalikan false

Disalin ke papan klip. Salin

Pada contoh pertama, kami mendapatkan kembali

// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
3 karena mungkin tidak semua produk diobral. Namun, kami dapat memeriksa apakah setiap produk memiliki nama, dalam hal ini
// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
7 akan
// We do have some products which are on sale so we get back true
products.some(product => product.onSale)

// None of the items are less than 0, so we get back false
[1, 2, 3].some(item => item < 0)

4 nilai
// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
2

Ingin meningkatkan keterampilan Anda?

Cara menggunakan foreach break javascript

Menggunakan Loop Reguler

Last but not least, kita juga dapat menggunakan loop reguler yang mendukung pernyataan

// We do have some products which are on sale so we get back true
products.some(product => product.onSale)

// None of the items are less than 0, so we get back false
[1, 2, 3].some(item => item < 0)

6 dan
// We do have some products which are on sale so we get back true
products.some(product => product.onSale)

// None of the items are less than 0, so we get back false
[1, 2, 3].some(item => item < 0)

7. Dalam contoh di bawah ini, kami menggunakan
// We do have some products which are on sale so we get back true
products.some(product => product.onSale)

// None of the items are less than 0, so we get back false
[1, 2, 3].some(item => item < 0)

8 reguler,
// We do have some products which are on sale so we get back true
products.some(product => product.onSale)

// None of the items are less than 0, so we get back false
[1, 2, 3].some(item => item < 0)

9, dan
// This produces false
products.every(product => product.onSale)

// This produces true
products.every(product => product.name)
0 loop untuk mendemonstrasikan cara memecahkan ketika pernyataan
// This will return 2
[1, 2, 3].findIndex(number => number === 3)

// This will return -1
[1, 2, 3].findIndex(number => number === 4)
2

const array = [1, 2, 3]

// Using a for loop
for (let i = 0; i < array.length; i++) {
    if (array[i] === 2) {
        break
    }

    console.log(array[i])
}

// Using a for...of loop
for (const item of array) {
    if (item === 2) {
        break
    }

    console.log(item)
}

// Using a while loop
let i = 0

while (i < array.length) {
    if (array[i] === 3) {
        break
    }

    console.log(i)
    i++
}
_ Menggunakan untuk, untuk. of dan while loop untuk melanggar

Disalin ke papan klip. Salin

Jika Anda juga ingin mempelajari lebih lanjut tentang metode array lainnya, seperti

// This produces false
products.every(product => product.onSale)

// This produces true
products.every(product => product.name)
2,
// This produces false
products.every(product => product.onSale)

// This produces true
products.every(product => product.name)
3 atau
// This produces false
products.every(product => product.onSale)

// This produces true
products.every(product => product.name)
4, pastikan Anda membaca artikel kami di bawah ini

Cara menggunakan foreach break javascript

Kekuatan Fungsi Array Tingkat Tinggi

temukan, beberapa, petakan, kurangi, masing-masing, filter
Pelajari semua yang perlu Anda ketahui tentang metode array tingkat tinggi, dan bagaimana Anda dapat menggunakannya secara maksimal