如何更改嵌套在对象数组中的对象的状态/ setState?

时间:2019-11-21 21:45:41

标签: javascript reactjs

我正在寻找在setState数组中用object命名为sms_group的{​​{1}}用collections更新数据数组的最佳方法。

我想这样做,并保持所有其他对象相同。

我想这样做而不引用collection索引,例如collection [2]。有人有建议吗?


class SomeComponent extends React.Component {
   constructor(props) {
      super(props);

      this.state={
                 collections: [
                {
                   id: 1,
                   name: 'messages',
                   label: 'Messages',
                   new_group: true,
                   new_chat: true,
                   new_friend: false,
                   data: [
                      {
                         id: 1,
                         name: 'Patsy Paulton',
                         status: 'Traditional heading elscas sdscsd sdcsdsc',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: true,
                         newMessageCount: 2,
                         avatarStatus: 'avatar-state-success'
                      },
                      {
                         id: 2,
                         name: 'Karl Hubane',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 3,
                         name: 'Entertainment Group',
                         status: '<strong>Maher Ruslandi: </strong>Hello!!!',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 4,
                         name: 'Jennica Kindred',
                         status: 'I know how important this file is to you. You can trust me ;)',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false,
                         avatarStatus: 'avatar-state-warning'
                      },
                      {
                         id: 5,
                         name: 'Marvin Rohan',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 6,
                         name: 'Frans Hanscombe',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      }
                   ]
                },
                {
                   id: 2,
                   name: 'sms_groups',
                   label: 'SMS Groups',
                   new_group: false,
                   new_chat: false,
                   new_friend: true,
                   data: [
                      {
                         id: 1,
                         name: 'Harrietta Souten',
                         status: 'Traditional heading elscas sdscsd sdcsdsc',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 2,
                         name: 'Aline McShee',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 3,
                         name: 'Brietta Blogg',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 4,
                         name: 'Karl Hubane',
                         status: 'I know how important this file is to you. You can trust me ;)',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 5,
                         name: 'Jillana Tows',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 6,
                         name: 'Alina Derington',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 7,
                         name: 'Stevy Kermeen',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 8,
                         name: 'Gloriane Shimmans',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 9,
                         name: 'Bernhard Perrett',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      }
                   ]
                },
                {
                   id: 3,
                   name: 'phone_numbers',
                   label: 'Phone Numbers',
                   new_group: false,
                   new_chat: false,
                   new_friend: false,
                   data: [
                      {
                         id: 1,
                         name: 'Jennica Kindred',
                         status: 'Traditional heading elscas sdscsd sdcsdsc',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 2,
                         name: 'Marvin Rohan',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 3,
                         name: 'Frans Hanscombe',
                         status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      },
                      {
                         id: 4,
                         name: 'Karl Hubane',
                         status: 'I know how important this file is to you. You can trust me ;)',
                         imageUrl: 'https://via.placeholder.com/150',
                         newMessage: false
                      }
                   ]
                }
             ]
    }

我要更改:

                   data: [
                  {
                     id: 1,
                     name: 'Harrietta Souten',
                     status: 'Traditional heading elscas sdscsd sdcsdsc',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 2,
                     name: 'Aline McShee',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 3,
                     name: 'Brietta Blogg',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 4,
                     name: 'Karl Hubane',
                     status: 'I know how important this file is to you. You can trust me ;)',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 5,
                     name: 'Jillana Tows',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 6,
                     name: 'Alina Derington',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 7,
                     name: 'Stevy Kermeen',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 8,
                     name: 'Gloriane Shimmans',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 9,
                     name: 'Bernhard Perrett',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  }
               ]

                   data: [
                  {
                     id: 1,
                     name: 'Harrietta Souten',
                     status: 'Traditional heading elscas sdscsd sdcsdsc',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 9,
                     name: 'Bernhard Perrett',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  }
               ]

3 个答案:

答案 0 :(得分:2)

您可以尝试以下方法:

const updateSmsGroups = () =>
 return state.collections.map( c => {
  if(c.name === 'sms_groups'){
    //Just make your modifications here
    c.new_group = true
   }
   return c
 } );
}

答案 1 :(得分:1)

无论如何,您都必须提供一个标识符来更新集合数组中的正确对象。

让我们尝试一下这个名字。

const { collections } = this.state

const updatedCollections = collections.reduce((acc, curr) => {
  if ( curr.name === 'sms_group' ) {
    // Update yourobject here
    curr.name = 'new_sms_group' // for example
    return [ ...acc, curr ]
  }

  return acc
}, [])

// or you can simply iterate on your collections array with map

const updatedCollections = collections.map(collection => {
  if ( collection.name === 'sms_group') {
    collection.name = 'new_sms_group'
  }
  return collection
}

this.setState({ collections: updatedCollections })

答案 2 :(得分:0)

该问题已得到大多数解答,但许多人都没有创建变量的副本。在这里查看此答案:Updating an object with setState in React

this.state ={
             collections: [
            {
               id: 1,
               name: 'messages',
               label: 'Messages',
               new_group: true,
               new_chat: true,
               new_friend: false,
               data: [
                  {
                     id: 1,
                     name: 'Patsy Paulton',
                     status: 'Traditional heading elscas sdscsd sdcsdsc',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: true,
                     newMessageCount: 2,
                     avatarStatus: 'avatar-state-success'
                  },
                  {
                     id: 2,
                     name: 'Karl Hubane',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 3,
                     name: 'Entertainment Group',
                     status: '<strong>Maher Ruslandi: </strong>Hello!!!',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 4,
                     name: 'Jennica Kindred',
                     status: 'I know how important this file is to you. You can trust me ;)',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false,
                     avatarStatus: 'avatar-state-warning'
                  },
                  {
                     id: 5,
                     name: 'Marvin Rohan',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 6,
                     name: 'Frans Hanscombe',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  }
               ]
            },
            {
               id: 2,
               name: 'sms_groups',
               label: 'SMS Groups',
               new_group: false,
               new_chat: false,
               new_friend: true,
               data: [
                  {
                     id: 1,
                     name: 'Harrietta Souten',
                     status: 'Traditional heading elscas sdscsd sdcsdsc',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 2,
                     name: 'Aline McShee',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 3,
                     name: 'Brietta Blogg',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 4,
                     name: 'Karl Hubane',
                     status: 'I know how important this file is to you. You can trust me ;)',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 5,
                     name: 'Jillana Tows',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 6,
                     name: 'Alina Derington',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 7,
                     name: 'Stevy Kermeen',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 8,
                     name: 'Gloriane Shimmans',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 9,
                     name: 'Bernhard Perrett',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  }
               ]
            },
            {
               id: 3,
               name: 'phone_numbers',
               label: 'Phone Numbers',
               new_group: false,
               new_chat: false,
               new_friend: false,
               data: [
                  {
                     id: 1,
                     name: 'Jennica Kindred',
                     status: 'Traditional heading elscas sdscsd sdcsdsc',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 2,
                     name: 'Marvin Rohan',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 3,
                     name: 'Frans Hanscombe',
                     status: 'Lorem ipsum dolor sitsdc sdcsdc sdcsdcs',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  },
                  {
                     id: 4,
                     name: 'Karl Hubane',
                     status: 'I know how important this file is to you. You can trust me ;)',
                     imageUrl: 'https://via.placeholder.com/150',
                     newMessage: false
                  }
               ]
            }
         ]
};


// create a copy of your state
const collections = Object.assign([], this.state.collections);
// do your changes
collections.forEach(cl => {
   if(cl.name === 'sms_groups') {
       // put your new data object here
       cl.data={};
   }
});

//this.setState({collections});
console.log(collections);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>