React Transferring Props except one

前端 未结 5 1975
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-23 13:05

React suggests to Transfer Props. Neat!

How can I transfert all but one?

render: function(){
  return (

        
5条回答
  •  情歌与酒
    2020-12-23 13:41

    Try this:

    function removeProps(obj, propsToRemove) {
       let newObj = {};
       Object.keys(obj).forEach(key => {
       if (propsToRemove.indexOf(key) === -1)
       newObj[key] = obj[key]
       })
       return newObj;
    }
    
    const obj = {nome: 'joao', tel: '123', cidade: 'goiania'}
    
    const restObject = removeProps(obj, ['cidade', 'tel'])
    
    console.log('restObject',restObject)
    
    restObject
    {
      nome:"joao"
    }
    

提交回复
热议问题