3 回答
TA贡献1821条经验 获得超6个赞
你可以试试这个:
const updateSmsGroups = () =>
return state.collections.map( c => {
if(c.name === 'sms_groups'){
//Just make your modifications here
c.new_group = true
}
return c
} );
}
TA贡献2065条经验 获得超14个赞
无论如何,您必须提供一个标识符来更新您的集合数组中的正确对象。
让我们试试这个名字。
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 })
TA贡献2036条经验 获得超8个赞
这个问题已经得到了大部分的回答,但是很多人错过了创建变量的副本。在此处查看此答案:在 React 中使用 setState 更新对象
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>
添加回答
举报