[英]Filter array using regex in React.js?
我希望能够检查并查看哪些键以字母“ gra”开头,并且仅返回该内容。 我已经尝试使用以下代码进行一些操作。 在一种方法中,我在返回html中添加了类似{key.match(/ gra / g)}的内容,但效率不高(仍然输出空白内容)。 我也尝试添加以下内容:
if(String(this.props.profile[key]).match(/gra/g)))
还尝试了:
if(this.props.profile[key].match(/gra/g))
但是不断出现诸如“ .match”之类的错误不是函数,或者无法读取未定义的字符,等等。我尝试了多种其他方式,但似乎无法使其正常工作。
var grapes = Object.keys(this.props.profile).map(function(key) {
if(this.props.profile[key] !=null) {
return (
<li key={key} className="example-class">
<label>{key}:</label>{ this.props.profile[key] }
</li>
);
}
}, this);
<html>
{grapes}
</html>
那么,这么说来,使用正则表达式进行此类过滤的最佳方法是什么?
为什么不只是过滤数组
var grapes = Object.keys(this.props.profile)
.filter(function(key) {
return key && key.toLowerCase().indexOf('gra') === 0;
})
.map(function(key) {
return (
<li key={key} className="example-class">
<label>{key}:</label>{ this.props.profile[key] }
</li>
);
}, this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.