簡體   English   中英

獲取數組中類型的第n項的索引

[英]Get the index of the nth item of a type in an array

我有一個可以看起來像這樣的數組:

let items = [
  1, '-', 231, '-', 6, 67, '-', 13, 177, 451, '-', 43, '-', 345, 56, 45
]

我想得到第n個-並在它之后返回所有內容。 所以我嘗試了這個,起初我認為它正在工作,但是當我看到它時,我看到它正在獲取索引然后從數組中的索引而不是短划線的索引。

因此,例如,如果n等於4,則它當前正在獲得第4個索引,而不是第4個指針處於索引10處。

 let items = [ 1, '-', 231, '-', 6, 67, '-', 13, 177, 451, '-', 43, '-', 345, 56, 45 ] // Get the number of dashes let stops = items.filter(i => i == '-').length // Select a random dash let n = Math.floor(Math.random() * stops) // find the dash at the random position, then get a list of items that are not a dash let sel = items.slice(n + 1).filter(r => r != '-') // Select the first 3 items for the final output let final = sel.slice(0, 3) console.log(n) console.log(final) 

您可以使用所需數字的Array#findIndex進行遞減,如果計數器達到零,則返回true

 const items = [1, '-', 231, '-', 6, 67, '-', 13, 177, 451, '-', 43, '-', 345, 56, 45], getNth = n => items.slice(items.findIndex(v => v === '-' && !--n) + 1); console.log(getNth(1)); console.log(getNth(2)); console.log(getNth(3)); console.log(getNth(4)); console.log(getNth(5)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

試試這個解決方案 代碼中的注釋描述。

 let items = [ 1, '-', 231, '-', 6, 67, '-', 13, 177, 451, '-', 43, '-', 345, 56, 45 ] let stops = []; // Push dashes indexes according to the `items` into the `stops`. items.forEach((item, index) => item === '-' ? stops.push(index) : false); // false is done only for one line body console.log(stops); // Select a random dash let n = Math.floor(Math.random() * stops.length); console.log(n); // find the dash at the random position and use that position to get from the `stops` array and use that value according to the `items`, then get a list of items that are not a dash let sel = items.slice(stops[n] + 1).filter(r => r !== '-'); // Select the first 3 items for the final output let final = sel.slice(0, 3); console.log(final); 

您可以將findIndex()slice()使用。

 let items = [ 1, '-', 231, '-', 6, 67, '-', 13, 177, 451, '-', 43, '-', 345, 56, 45 ] function getNth(data, n) { let c = 0; let i = data.findIndex(e => e == '-' && ++c == n ? true : false); return i != -1 ? data.slice(i + 1) : undefined; } console.log(JSON.stringify(getNth(items, 2))) console.log(JSON.stringify(getNth(items, 1))) console.log(JSON.stringify(getNth(items, 4))) console.log(JSON.stringify(getNth(items, 100))) 

 let items = [ 1, '-', 231, '-', 6, 67, '-', 13, 177, 451, '-', 43, '-', 345, 56, 45 ] function getNth(value){ var count = 0; var myIndex; items.forEach((item, index) => { if(item === "-"){ count++; if(count === value ){ myIndex = index; } } }) return items.slice(myIndex); } console.log(getNth(2)) console.log(getNth(3)) 

 let items = [ 1, '-', 231, '-', 6, 67, '-', 13, 177, 451, '-', 43, '-', 345, 56, 45 ] let n = 3 // nth item let count = 0 let index for(i = 0; i < items.length; i++) { if(items[i] == '-') { count++ } if(count == n) { index = i + 1 break; } } let final = items.slice(index, items.length + 1) console.log(JSON.stringify(final)) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM