【发布时间】:2021-11-24 16:41:05
【问题描述】:
与我上面建议的用例有点不同。 我需要遍历并检查文件数组中的每个文件名,并将具有相同名称的文件推送到新数组中,以便稍后单独上传。
到目前为止,这是我的代码,我的条件检查肯定有问题,有人能看出我做错了什么吗?
filesForStorage = [
{id: 12323, name: 'name', ...},
{id: 3123, name: 'abc', ...},
{id: 3213, name: 'name', ...},
...
]
filesForStorage.map((image, index) => {
for (let i = 0; i < filesForStorage.length; i++) {
for (let j = 0; j < filesForStorage.length; j++) {
if (
filesForStorage[i].name.split(".", 1) ===. //.split('.', 1) is to not keep in consideration the file extension
filesForStorage[j].name.split(".", 1)
) {
console.log(
"----FILES HAVE THE SAME NAME " +
filesForStorage[i] +
" " +
filesForStorage[j]
);
}
}
}
【问题讨论】:
-
你能分享一下filesForStorage的演示数据吗?一个 sn-p 会很棒。
-
为什么要在
Array.map中运行2个for循环? -
当然,数组和文件是这样的:Array (10) 0 File {id: 0.6385802192553822, preview: "blob:localhost:3000/c857aaa7-51dd-4d8a-b209-810b9eaf9d34", uniqueId: "8Am0GOPEwATyd0ZkSHlG", name: "background1. jpeg", lastModified: 1624555546000, ...} 1 文件 {live: true, id: 0.6403926850353915, preview: "blob:localhost:3000/e6db45de-cd66-408e-8c55-ac9a9968d504", uniqueId: "6hPqArZp1nNqruDIbh74", name: "image.gif", ...} 2 File {id : 0.5735947653890782, 预览: "blob:localhost:3000/697954be-50ee-46e6-ba38-8d6be54e7a9d", 名称: "background.png", ... }, ...]
-
我正在映射,因为我在用于将每个文件发送到我的数据库的同一函数中执行此操作,我之前也可以这样做。
-
@Jacopo 请格式化它们并将它们添加到问题中
标签: javascript reactjs loops if-statement recursion