React.js right way to iterate over object instead of Object.entries

前端 未结 2 549
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 11:25

I don\'t like using Object.entries(object).map((key, i) because I found out that this is an experimental technology of ECMAScript 7 and I feel that something ma

相关标签:
2条回答
  • 2020-12-23 12:02

    Complete function render in react

    const renderbase = ({datalist}) => {
            if(datalist){
                return  Object.keys(datalist).map((item,index) => {
                    return(
                      <option value={datalist[item].code} key={index}>
                          {datalist[item].symbol}
                      </option>
                    )
                })
            }  
        }
    
    0 讨论(0)
  • 2020-12-23 12:20
    a = { 
      a: 1,
      b: 2,
      c: 3
    }
    
    Object.keys(a).map(function(keyName, keyIndex) {
      // use keyName to get current key's name
      // and a[keyName] to get its value
    })
    

    A newer version, using destructuring and arrow functions. I'd use this one for new code:

    a = { 
      a: 1,
      b: 2,
      c: 3
    }
    
    Object.entries(a).map(([key, value]) => {
        // Pretty straightforward - use key for the key and value for the value.
        // Just to clarify: unlike object destructuring, the parameter names don't matter here.
    })
    
    0 讨论(0)
提交回复
热议问题