0

I am trying to display key and value from a json object. here is the code i m using

var FirebaseItem = React.createClass({
  render: function() {
    var v=[];var k=[];
    var foo;
      for(var key in this.props.data){
        v.push(this.props.data[key]);
        k.push(key);

      }
       foo= function(){
          return <p>{k} : {v}</p>;
        }();
    return (
           <div className="Data-item">{foo}</div>
    );
  }
});

var FirebaseList = React.createClass({
  render: function() {
    var FirebaseData = this.props.data.map(function (obj) {
      return (
           <FirebaseItem data = {obj}/>
      );
    });
    return (
           <div className="Datalist">
        {FirebaseData}
      </div>  
    );
  }
});


var Firebase = React.createClass({
  render: function() {
    var FirebaseData=this.props.data;

    return (
<FirebaseList data={FirebaseData}/>

    );
  }
})
var JsonData = [
  {author: "Pete Hunt", text: "This is one comment"},
  {author: "Jordan Walke", text: "This is *another* comment"},
  {author: "jone doe", text: "This is one comment"}
];
React.render(

  <Firebase data={JsonData} />,
  document.getElementById('Datawrap')
);

Here i am getting response like

authortext : Pete HuntThis is one comment

authortext : Jordan WalkeThis is another comment

authortext : jone doeThis is one comment

Thanks in advance

1 Answer 1

1

I think you want something like that:

var FirebaseItem = React.createClass({
  render: function() {
    var pairs = [];
      for(var key in this.props.data){
        pairs.push(<p>{key} : {this.props.data[key]}</p>);
      }
    return (
           <div className="Data-item">{pairs}</div>
    );
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks just staring with react,in addition to it want to ask in case of complex json how to achieve this .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.