如何循环,返回和呈现对象

时间:2017-07-23 10:49:27

标签: javascript reactjs firebase firebase-realtime-database

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

{
  "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"
  }
  }

}

我以这种形式初始化我的状态:

this.state={
      startups:[]

在这里,我打电话给firebase来设置我的状态:

 componentDidMount(){
    const rootRef = firebase.database().ref().child('startups')
    rootRef.once('value', snap =>{
      this.setState({

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

      });
    }

);

} 我试过以不同的方式循环: 1。

formatStartUps(){
   const startups = [this.state.startups];
   startups.forEach((element)=>{console.log(element)} )
 }

2

formatStartUps(){
       const startups = [this.state.startups];
       startups.map((startup,index)=>{<p key={index}>{startup}</p>))
     }

3

formatStartUps(){
           const startups = [this.state.startups];
           for(let startup of startups){
            console.log(startup)
            }
         }

然后我调用fire-base数据库来设置我的状态并运行,但我无法循环到每个启动以在div中呈现这些值。

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

3 个答案:

答案 0 :(得分:1)

如果# my pool code def sums_to(nums, k): if nums == []: return False if nums[0] + sums_to(nums[1:], k) == k: return True 是一个对象

startups

答案 1 :(得分:1)

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

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);
  
})

答案 2 :(得分:0)

你可以做那样的事情

df %>%
  group_by(Number) %>%
  filter(n() > 1) %>%
  summarize(ship_no = paste0(unique(ship_no), collapse = ','))

有关详细信息,请参阅es6

中的map或forEAch