我是JS的新手并且有以下对象:key
是文件的路径,value
是文件名和时间它被添加了。这些文件是重复的,名称相同,即 JackFusco_JasperGeminids.jpg 和 Goldman-Sachs-Suggested-Reading-List.pdf
{"/home/JackFusco_JasperGeminids.jpg": ["JackFusco_JasperGeminids.jpg", "2018-02-23T02:43:21"],
"/Photos/JackFusco_JasperGeminids.jpg": ["JackFusco_JasperGeminids.jpg", "2018-02-23T02:43:42"],
"/home/Goldman-Sachs-Suggested-Reading-List.pdf": ["Goldman-Sachs-Suggested-Reading-List.pdf", "2018-02-23T02:01:24"],
"/Photos/Sample Album/Goldman-Sachs-Suggested-Reading-List.pdf": ["Goldman-Sachs-Suggested-Reading-List.pdf", "2018-02-23T02:01:56"]}
我想创建另一个对象并基本上组合相同的名称文件,以告诉用户相同的文件存储在两个不同的位置。
预期输出为:
{"JackFusco_JasperGeminids.jpg": ["/home/JackFusco_JasperGeminids.jpg", "2018-02-23T02:43:21"], ["/Photos/JackFusco_JasperGeminids.jpg", "2018-02-23T02:43:42"]}
{"Goldman-Sachs-Suggested-Reading-List.pdf": ["/home/Goldman-Sachs-Suggested-Reading-List", "2018-02-23T02:01:24"], ["/Photos/Sample Album/Goldman-Sachs-Suggested-Reading-List.pdf", "2018-02-23T02:01:56"]}
答案 0 :(得分:1)
您的输入
var input = {"/home/JackFusco_JasperGeminids.jpg": ["JackFusco_JasperGeminids.jpg", "2018-02-23T02:43:21"],
"/Photos/JackFusco_JasperGeminids.jpg": ["JackFusco_JasperGeminids.jpg", "2018-02-23T02:43:42"],
"/home/Goldman-Sachs-Suggested-Reading-List.pdf": ["Goldman-Sachs-Suggested-Reading-List.pdf", "2018-02-23T02:01:24"],
"/Photos/Sample Album/Goldman-Sachs-Suggested-Reading-List.pdf": ["Goldman-Sachs-Suggested-Reading-List.pdf", "2018-02-23T02:01:56"]};
解决方案(在定义"输入"
之后)var result = {};
for (var key in input){
if(!result.hasOwnProperty(input[key][0])) result[input[key][0]] = [];
result[input[key][0]].push(key);
}
result
result
是否已有与您的文件名匹配的属性。如果它没有创建这样的属性,并将其初始化为数组。