Return 个来自映射函数的对象(如果为真)
Return objects from a map function if true
我想 return 仅在检查文档时从地图函数中获取对象。这是我所在州的结构。
{
type: ListOfTypes[Math.floor(Math.random() * ListOfTypes.length)],
name: ListOfNames[Math.floor(Math.random() * ListOfNames.length)],
id: nanoid(),
channels: [
{
id: nanoid(),
Name: ListOfChannels[Math.floor(Math.random() * ListOfChannels.length)],
Files: [
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },],
},
{
id: nanoid(),
Name: ListOfChannels[Math.floor(Math.random() * ListOfChannels.length)],
Files: [
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
],
},
{
id: nanoid(),
Name: ListOfChannels[Math.floor(Math.random() * ListOfChannels.length)],
Files: [
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
],
}
]
}
我试图通过状态数组进行映射,但我使用它得到了一个包含不必要数组的对象。
const handleMoveAll = () => {
const CheckedDocuments = List.map( (connection: any) => (connection.id == connectionId) &&
connection.channels.map( (channel: any) => (channel.id == channelId) &&
channel.Files.map( (file: any) =>
file.documents.map( (doc: any) => doc.isChecked &&
doc
))
)
)
console.log(CheckedDocuments)
}
是否有更好的方法来遍历 Files 数组以检查已检查的文档并将它们return合并到一个数组中?
感谢@HagaiHarari 和@oakar。
const handleMoveAll = () => {
const CheckedDocuments = List.flatMap((connection: any) => (connection.id == connectionId) ?
connection.channels.flatMap((channel: any) => (channel.id == channelId) ?
channel.Files.flatMap( (file: any) =>
file.documents.filter( (doc: any) => { if(doc.isChecked) return doc }
))
: []
)
: []
)
console.log(CheckedDocuments)
}
我想 return 仅在检查文档时从地图函数中获取对象。这是我所在州的结构。
{
type: ListOfTypes[Math.floor(Math.random() * ListOfTypes.length)],
name: ListOfNames[Math.floor(Math.random() * ListOfNames.length)],
id: nanoid(),
channels: [
{
id: nanoid(),
Name: ListOfChannels[Math.floor(Math.random() * ListOfChannels.length)],
Files: [
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },],
},
{
id: nanoid(),
Name: ListOfChannels[Math.floor(Math.random() * ListOfChannels.length)],
Files: [
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
],
},
{
id: nanoid(),
Name: ListOfChannels[Math.floor(Math.random() * ListOfChannels.length)],
Files: [
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
{ folder: "Folder", id: nanoid(), documents: [{ doc: "WordDoc1.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc2.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc3.doc", isChecked: false, id: nanoid() }, { doc: "WordDoc4.doc", isChecked: false, id: nanoid() }] },
],
}
]
}
我试图通过状态数组进行映射,但我使用它得到了一个包含不必要数组的对象。
const handleMoveAll = () => {
const CheckedDocuments = List.map( (connection: any) => (connection.id == connectionId) &&
connection.channels.map( (channel: any) => (channel.id == channelId) &&
channel.Files.map( (file: any) =>
file.documents.map( (doc: any) => doc.isChecked &&
doc
))
)
)
console.log(CheckedDocuments)
}
是否有更好的方法来遍历 Files 数组以检查已检查的文档并将它们return合并到一个数组中?
感谢@HagaiHarari 和@oakar。
const handleMoveAll = () => {
const CheckedDocuments = List.flatMap((connection: any) => (connection.id == connectionId) ?
connection.channels.flatMap((channel: any) => (channel.id == channelId) ?
channel.Files.flatMap( (file: any) =>
file.documents.filter( (doc: any) => { if(doc.isChecked) return doc }
))
: []
)
: []
)
console.log(CheckedDocuments)
}