最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to iterate nested objects and render inside jsx? - Stack Overflow

programmeradmin1浏览0评论

How can I render a nested map inside my jsx component?

I need to do the equivalent of a javascript for(key in groupItem){} See below.

class MyComponent extends React.Component {
    render () {    
        var options = this.props.options;
        return (            
            <div>
                {options.map(function(groupItem, key){ return (
                    /* 
                      Unexpected Token if using groupItem.map?
                      {groupItem.map(function(){return })}

                   */
                )})}
            </div>
        )
    }
}
Dropdown.defaultProps = {
   options:[{ 
      'groupX':{
          'apple':'lovely green apple',
          'orange':'juicy orange',
          'banana':'fat banana'
      }
   }]
}

JSON.stringify(groupItems) === {
    'groupX':{
        'apple':'lovely green apple',
        'orange':'juicy orange',
        'banana':'fat banana'
     }
}

WHY DON'T THESE WORK?

groupItem.map - DOESN'T WORK

Object.keys(groupItem).forEach(function (key){ - DOESN'T WORK

How can I render a nested map inside my jsx component?

I need to do the equivalent of a javascript for(key in groupItem){} See below.

class MyComponent extends React.Component {
    render () {    
        var options = this.props.options;
        return (            
            <div>
                {options.map(function(groupItem, key){ return (
                    /* 
                      Unexpected Token if using groupItem.map?
                      {groupItem.map(function(){return })}

                   */
                )})}
            </div>
        )
    }
}
Dropdown.defaultProps = {
   options:[{ 
      'groupX':{
          'apple':'lovely green apple',
          'orange':'juicy orange',
          'banana':'fat banana'
      }
   }]
}

JSON.stringify(groupItems) === {
    'groupX':{
        'apple':'lovely green apple',
        'orange':'juicy orange',
        'banana':'fat banana'
     }
}

WHY DON'T THESE WORK?

groupItem.map - DOESN'T WORK

Object.keys(groupItem).forEach(function (key){ - DOESN'T WORK

Share Improve this question asked Jun 27, 2016 at 14:21 InspirallerInspiraller 3,8066 gold badges38 silver badges79 bronze badges 1
  • It's Object.keys + map, not forEach. forEach is meant to simply iterate. map retains returned values and creates a new array. – Gosha A Commented Jun 27, 2016 at 15:13
Add a comment  | 

2 Answers 2

Reset to default 16

You were almost right with your Object.keys implementation, (map is a property for arrays only), but the syntax error is coming from the wrapping {}. You don't need to escape, you're already inside js syntax.

return (            
    <div>
        {options.map(function(groupItem, key){ return (
            Object.keys(groupItem).map(function(item){return (
                <YourComponent group={groupItem} item={item} />
            );})
        );})}
    </div>
);

I've decided to create my own method, due to the clunkyness of this.

function each(obj, callback){
    return (Array.isArray(obj))? forArray(obj, callback):forObject(obj, callback);
}
function forArray(obj, callback){
    return obj.map(callback);
}
function forObject(obj, callback){
    return Object.keys(obj).map(callback);
}


class MyComponent extends React.Component {
    render () {    
        var options = this.props.options;
        return (            
            <div>                    
                {each(options, function(groupItem){ return (                        
                     each(groupItem, function(key){return ( 

This is much easier to read.

发布评论

评论列表(0)

  1. 暂无评论