试图遍历数组,我遇到以下错误: TypeError:对象null不可迭代(无法读取属性Symbol(Symbol.iterator))
我正在使用下面的getHighlightedText函数突出显示我的Search值。我的代码:
getHighlightedText = text => {
const {value} = this.props;
if (value == "" || value == null || value == 0) {
return <Text> {text} </Text>
} else {
const words = value.split(/\s+/g).filter(word => word.length);
const pattern = words.join('|');
const re = new RegExp(pattern, 'gi')
const children = [];
let before, highlighted, match, pos = 0;
const matches = text.match(re);
for (match of matches) { // guess this the error location
match = re.exec(text)
if (pos < match.index) {
before = text.substring(pos, match.index);
if (before.length) {
children.push(before)
}
}
highlighted = <Text style={{backgroundColor: 'coral'}}>{match[0]} </Text>
children.push(highlighted);
pos = match.index + match[0].length;
}
if (pos < text.length) {
const last = text.substring(pos);
children.push(last);
}
return <Text>{children} </Text>
}
}
如何解决此问题?有任何想法吗???