首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在react中循环、返回和呈现对象

如何在react中循环、返回和呈现对象
EN

Stack Overflow用户
提问于 2017-07-23 18:49:27
回答 3查看 146关注 0票数 0

我有一个对象,看起来像这样:

代码语言:javascript
复制
{
  "startups" : {
    "startup0":{
    "achievements" : "Is done!",
    "how_build" : "In python and using google visio api",
    "inspiration" : "All the hot dogs in the world",
    "proyect_name" : "Hog dog or not Hot dog",
    "team" : {
      "Steven Anderson" : {
        "area" : "CEO",
        "email" : "sdandersonz97@gmail.com",
        "expertise" : "full-stack engineer"
      }
    },
    "what_does" : "Say is the image is a hot dog or not"
  },
  "startup1":{
    "achievements" : "Is done!",
    "how_build" : "In python and using google visio api",
    "inspiration" : "All the hot dogs in the world",
    "proyect_name" : "Big Brother",
    "team" : {
      "Steven Anderson" : {
        "area" : "CEO",
        "email" : "sdandersonz97@gmail.com",
        "expertise" : "full-stack engineer"
      }
    },
    "what_does" : "Say is the image is a hot dog or not"
  }
  }

}

我用以下形式初始化我的状态:

代码语言:javascript
复制
this.state={
      startups:[]

这里我调用firebase来设置我的状态:

代码语言:javascript
复制
 componentDidMount(){
    const rootRef = firebase.database().ref().child('startups')
    rootRef.once('value', snap =>{
      this.setState({

        startups: this.state.startups.push(snap.val())

      });
    }

);

}我尝试过不同的循环方式: 1.

代码语言:javascript
复制
formatStartUps(){
   const startups = [this.state.startups];
   startups.forEach((element)=>{console.log(element)} )
 }

2.

代码语言:javascript
复制
formatStartUps(){
       const startups = [this.state.startups];
       startups.map((startup,index)=>{<p key={index}>{startup}</p>))
     }

3.

代码语言:javascript
复制
formatStartUps(){
           const startups = [this.state.startups];
           for(let startup of startups){
            console.log(startup)
            }
         }

然后我调用基于fire的数据库来设置我的状态和工作,但是我不能循环到每次启动来在我的div中呈现这些值。

如何循环此对象并在render()方法中呈现每个值?我很感谢你的帮助

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-07-23 19:08:36

如果startups一个对象

代码语言:javascript
复制
Object.keys(startups).map((startup) => {
  return (
    <div>{startups[startup].proyect_name}</div> //for example
  )
})
票数 1
EN

Stack Overflow用户

发布于 2017-07-23 19:12:07

使用:Object.keys(objectName)循环对象

代码语言:javascript
复制
var data = {
  "startups" : {
    "startup0":{
    "achievements" : "Is done!",
    "how_build" : "In python and using google visio api",
    "inspiration" : "All the hot dogs in the world",
    "proyect_name" : "Hog dog or not Hot dog",
    "team" : {
      "Steven Anderson" : {
        "area" : "CEO",
        "email" : "sdandersonz97@gmail.com",
        "expertise" : "full-stack engineer"
      }
    },
    "what_does" : "Say is the image is a hot dog or not"
  },
  "startup1":{
    "achievements" : "Is done!",
    "how_build" : "In python and using google visio api",
    "inspiration" : "All the hot dogs in the world",
    "proyect_name" : "Big Brother",
    "team" : {
      "Steven Anderson" : {
        "area" : "CEO",
        "email" : "sdandersonz97@gmail.com",
        "expertise" : "full-stack engineer"
      }
    },
    "what_does" : "Say is the image is a hot dog or not"
  }
  }

}

Object.keys(data.startups).map(function(property){
   console.log(data.startups[property].achievements);
  
})

票数 1
EN

Stack Overflow用户

发布于 2017-07-23 18:55:54

你可以做这样的事情

代码语言:javascript
复制
render{
    return(
       <div>
        {
    this.state.startups.map(function(startups,i)
      {

    <li key={i}>{startups.achievements}</li>//in whatever element you want to print

      },this)
         }


        </div>
 )
 }

有关更多详细信息,请参阅es6中的地图或forEAch

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45264223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档