Answer the question
In order to leave comments, you need to log in
How to iterate over an array of objects and change a property inside the desired object, which also represents an array of objects?
I have the following state structure:
{
articles: [
{
id: '1AAAA',
title: 'AAAA',
summary: 'minimalAAA',
text: 'dsffsfsegrsgsrgsrs',
comments: [
{ id: '4cdrbdrbvoido',
name: 'Den',
comment: 'Ljbelkfei nfien nfien nfien nfien nfien nfien nfien nfien'},
{id: '5vjdbnvsjm;slv',
name: 'Ben',
comment: 'Ljbelkfei nfien nfien nfien nfien nfien nfien nfien nfien' }
]
},
{
id: '2BBBBB',
title: 'BBBBB',
summary: 'minimalBBB',
text: 'dsffsfsegrsgsrgsrs',
comments: [
{ id: '1cudohvoido',
name: 'Sem',
comment: 'Hnefei nfien nfien nfien nfien nfien nfien nfien nfien'},
{id: '2knsdknvpdo',
name: 'Pen',
comment: 'Hnefei nfien nfien nfien nfien nfien nfien nfien nfien'}
]
}
]
};
const commentDelete = (idComment) => {
return {
type: 'COMMENT_DELETE',
id:idComment,
};
};
case 'ARTICLES_DELETE':
const delArt = state.articles.filter(user => (user.id !== action.id));
return {
...state,
articles: [ ...delArt]
};
Answer the question
In order to leave comments, you need to log in
return {
...state,
articles: state.articles.map(({comments, ...o}) => ({
...o,
comments: comments.filter((c) => c.id !== action.id)
}))
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question