我建议使用Map.prototype.some
这个。它将短路并尽快返回true
为您的拉姆达返回truthy值 - 否则返回false
const { Map } = require('immutable')
const m = Map({a: 'one', b: 'two', c: 'three'})
m.some(v => /t/.test(v)) // true, at least one value has a 't'
m.some(v => /x/.text(v)) // false, no values contain an 'x'
// but be careful with automatic string coercion
/o/.test({}) // true, because String({}), '[object Object]', contains 'o'
如果你的地图将举行多种价值类型,你将要小心使用String.prototype
方法 - 即我会建议对这样的事情
const { Map } = require('immutable')
// mixed value type Map
const m = Map({a: 'one', b: 2, c: 3})
// CAUTION!
// reckless assumption that every value is a string (or has a .includes method)
m.some(v => v.includes('o')) // true, short-circuits on first value
m.some(v => v.includes('x')) // TypeError: v.includes is not a function
如果必须使用String.prototype.includes
,我会建议你做type
首先检查
const { Map } = require('immutable')
const m = Map({a: 'one', b: 2, c: 3})
m.some(v => typeof v === 'string' && v.includes('o')) // true
m.some(v => typeof v === 'string' && v.includes('x')) // fase
太棒了!对包含的重要建议。 – Colin