Saya tidak faham mengapa IndexOf mengembalikan -1
P粉031492081
P粉031492081 2023-09-14 20:02:21
0
1
501

Saya mempunyai susunan nombor yang mengandungi 1-20. Apabila saya menggunakan kaedah indexOf untuk cuba mencari kedudukan "2", ia muncul pada kedudukan ke-3 (betul).

Saya ada kawan bernamaday = 2的变量。当我使用array.indexOf(day)的代码时,它返回-1.

Mengapa ini berlaku?

P粉031492081
P粉031492081

membalas semua (1)
P粉517090748
function indexofnotworking() { let arr = [...new Array(20).keys()];//creat arr of numbers from 0 -19 let arr1 = arr.slice().reverse();//make a copy and reverse it [...Array.from(new Array(5).keys(), x => x + 5)].forEach(n => { console.log('arr: %s n: %s index: %s', JSON.stringify(arr), n, arr.indexOf(n));//find the indexof n in both arrays console.log('arr1: %s n: %s index: %s', JSON.stringify(arr1), n, arr1.indexOf(n));/display results }) } 11:40:22 AM Info arr: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19] n: 5 index: 5 11:40:22 AM Info arr1: [19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0] n: 5 index: 14 11:40:22 AM Info arr: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19] n: 6 index: 6 11:40:22 AM Info arr1: [19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0] n: 6 index: 13 11:40:22 AM Info arr: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19] n: 7 index: 7 11:40:22 AM Info arr1: [19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0] n: 7 index: 12 11:40:22 AM Info arr: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19] n: 8 index: 8 11:40:22 AM Info arr1: [19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0] n: 8 index: 11 11:40:22 AM Info arr: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19] n: 9 index: 9 11:40:22 AM Info arr1: [19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0] n: 9 index: 10
    Muat turun terkini
    Lagi>
    kesan web
    Kod sumber laman web
    Bahan laman web
    Templat hujung hadapan
    Tentang kita Penafian Sitemap
    Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!