React-按对象属性过滤 [英] React - filter by object property

查看:74
本文介绍了React-按对象属性过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按属性过滤对象,但是我无法使其工作.

I am trying to filter object by property but I can't make it work.

对象中的数据的结构如下:

我正在通过UID提取数据,然后映射该对象中的所有项目,但是我无法使过滤器正常工作.

I am fetching data by the UID and then mapping all the items from that object but I can't make the filter work.

渲染方法如下:

  render() {
    return(
      <div>
        {Object.keys(this.state.dataGoal)
          .filter(key => key.main == true)
          .map( (key, index) => {
            return <div key={key}>
                     <h1>{this.state.dataGoal[key].name}</h1>
                     <p>{this.state.dataGoal[key].main}</p>
                   </div>
          })}
      </div>

有任何想法我在做什么错吗?

Any ideas what I am doing wrong?

感谢您的帮助, 雅各布

Thanks for any help, Jakub

推荐答案

Object.keys返回该对象的keys,这意味着它将返回包含该对象中每个键的字符串数组.

Object.keys returns the keys of that object, meaning that it returns an array of strings with every key in that object.

obj = { 'a': 1, 'b': 2 };
Object.keys(obj); // ['a', 'b']

因此,要访问该属性的值,您必须使用该键来访问它,在您的情况下,将是这样的:

So to access the value of that property you have to access it using that key, in your case it would be something like this:

filter(key => this.state.dataGoal[key].main == true)

这篇关于React-按对象属性过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆