簡體   English   中英

Array.Some 必須至少包含一個填充值

[英]Array.Some must contain at least one populated value

SI 正在用 array.prototype.some() 練習一些東西

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

基本上我想要做的是只要一個數組包含一個填充的字符串值和一些空字符串值。 例如,只要數組中有一個填充的字符串值,我就希望它為真。

const array1 = [ 'g', '']; //returns true
const array2 = [ '', '']; //return true
const array3 = [ '', 'x']; //return true
const array4 = [ 'g', 'x']; //return false
// checks whether an element contains at least one populated string (non blank string)
const even = (element) => element === '';

console.log(array1.some(even)); //Should be True
console.log(array2.some(even)); //Should be False
console.log(array3.some(even)); //Should be True
console.log(array4.some(even)); //Should be True

我知道它是因為元素 === '' 但我不知道該寫什么才能使它成為我想要的樣子,提前致謝!

反轉測試 - 檢查某些元素是否不是空字符串:

 const array1 = [ 'g', '']; //returns true const array2 = [ '', '']; //return true const array3 = [ '', 'x']; //return true const array4 = [ 'g', 'x']; //return false // checks whether an element contains at least one populated string (non blank string) const even = (element) => element !== ''; console.log(array1.some(even)); //Should be True console.log(array2.some(even)); //Should be False console.log(array3.some(even)); //Should be True console.log(array4.some(even)); //Should be True

暫無
暫無

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

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