I'm struggle with using filter.
Initial object
This is my object.
[{
isOpen : true,
items : [
{itemId : '', name : 'some name'},
{itemId : 'new', name : 'new name'}
]
},]
Expected Result
I just want to show that the itemId
is not an empty string and also isOpen
field.
[{
isOpen : true,
items : [
{itemId : 'new', name : 'new name'}
]
},]
What I try
const removeEmptyStringInItems = (item) => item.items?.filter((v) => v.itemId);
const finalItems = myItems.map(removeEmptyStringInItems);
output .
{itemId : 'new', name : 'new name'}
Any ideas for this problem? I really need help!
you could try update the code to this to keep the finalItems format same as the initial value
const removeEmptyStringInItems = (items = []) => items?.filter((v) => v.itemId);
const finalItems = myItems.map((v) => ({...v, items: removeEmptyStringInItems(v.items)}));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.