Javascript JS,字典列表到字典列表,基于键
我有一个字典列表,它有一些属性,比如url和一些关于url的信息:Javascript JS,字典列表到字典列表,基于键,javascript,node.js,dictionary,Javascript,Node.js,Dictionary,我有一个字典列表,它有一些属性,比如url和一些关于url的信息: [{ url:"https://example1.com/a" something:"ABC" },{ url:"https://example1.com/b" something:"DEF" },{ url:"https://example2.com/c" something:"GHI" },{ url:"https://example2.com/d" somet
[{
url:"https://example1.com/a"
something:"ABC"
},{
url:"https://example1.com/b"
something:"DEF"
},{
url:"https://example2.com/c"
something:"GHI"
},{
url:"https://example2.com/d"
something:"JKL"
}]
现在我想将其拆分为一个列表字典,根据url进行分组。对于以上内容,我的目标数据结构如下:
{
"example1.com" : [{
url:"https://example1.com/a"
something:"ABC"
},{
url:"https://example1.com/b"
something:"DEF"
}],
"example2.com" : [{
url:"https://example2.com/c"
something:"GHI"
},{
url:"https://example2.com/d"
something:"JKL"
}]
}
在python中,这可能是通过使用itertools包和一些列表理解技巧实现的,但我需要在javascript/nodejs中实现
有人能指导我在javascript中做这件事的正确方向吗
干杯。
const dataFromQuestion=[{
网址:https://example1.com/a,
某物:ABC
},{
网址:https://example1.com/b,
某物:DEF
},{
网址:https://example2.com/c,
某物:GHI
},{
网址:https://example2.com/d,
某物:JKL
}];
DictionaryToDictionaryOflistInput、keyMapper的函数列表{
const result={};
用于输入的常量输入{
const key=keyMapperentry;
如果!Object.prototype.hasOwnProperty.callresult,键{
结果[键]=[];
}
结果[key].pushentry;
}
返回结果;
}
函数getHostdata{
const url=new URLdata.url;
返回url.host;
}
console.loglistOfDictionaryToDictionaryOfListdataFromQuestion,getHost
const dataFromQuestion=[{
网址:https://example1.com/a,
某物:ABC
},{
网址:https://example1.com/b,
某物:DEF
},{
网址:https://example2.com/c,
某物:GHI
},{
网址:https://example2.com/d,
某物:JKL
}];
DictionaryToDictionaryOflistInput、keyMapper的函数列表{
const result={};
用于输入的常量输入{
const key=keyMapperentry;
如果!Object.prototype.hasOwnProperty.callresult,键{
结果[键]=[];
}
结果[key].pushentry;
}
返回结果;
}
函数getHostdata{
const url=new URLdata.url;
返回url.host;
}
console.loglistOfDictionaryToDictionaryOfListdataFromQuestion,getHost 可以对数组对象使用reduce方法
let data = [{
url:"https://example1.com/a",
something:"ABC"
},{
url:"https://example1.com/b",
something:"DEF"
},{
url:"https://example2.com/c",
something:"GHI"
},{
url:"https://example2.com/d",
something:"JKL"
}];
let ret = data.reduce((acc, cur) => {
let host = cur['url'].substring(8, 20); // hardcoded please use your own
if (acc[host])
acc[host].push(cur);
else
acc[host] = [cur];
return acc;
}, {})
console.log(ret);
可以对数组对象使用reduce方法
let data = [{
url:"https://example1.com/a",
something:"ABC"
},{
url:"https://example1.com/b",
something:"DEF"
},{
url:"https://example2.com/c",
something:"GHI"
},{
url:"https://example2.com/d",
something:"JKL"
}];
let ret = data.reduce((acc, cur) => {
let host = cur['url'].substring(8, 20); // hardcoded please use your own
if (acc[host])
acc[host].push(cur);
else
acc[host] = [cur];
return acc;
}, {})
console.log(ret);
data.ReduceGroup,项=>{
让host=new URLitem.url.hostname;
组[主机]| |组[主机]=[].pushitem;
返回组;
}, {};
不过有一行很神秘
data.reduceg,i,_1,_2,h=newurli.url.hostname=>g[h]| | g[h]=[].pushi,g,{};
data.ReduceGroup,项=>{
让host=new URLitem.url.hostname;
组[主机]| |组[主机]=[].pushitem;
返回组;
}, {};
不过有一行很神秘
data.reduceg,i,_1,_2,h=newurli.url.hostname=>g[h]| | g[h]=[].pushi,g,{};
您尝试过什么吗?如何按值对对象属性进行分组已经被讨论过了。你肯定能找到你问题的现有答案吗?这些广泛的问题可能会用许多不同的方法来回答,你真的应该先尝试一些东西,然后就你的代码遇到的问题问一个具体的问题。你尝试过什么吗?如何按值对对象属性进行分组一直是个难题。你肯定能找到你问题的现有答案吗?这些广泛的问题可能会用许多不同的方法来回答,你真的应该先尝试一些东西,然后就你的代码遇到的问题问一个具体的问题。是的。就这样。我可以肯定地说。干杯。结果[键]=结果[键]| |[]。按回车键;如果将result初始化为let result=Object.createnull,则可以省去hasOwnProperty测试,因为result将只具有自己的属性。和Object.prototype.hasOwnProperty.callresult,key似乎不必要的冗长,为什么不result.hasOwnPropertykey?@RobG我不想假设OP的其余程序能够处理空原型对象。至于Object.prototype.hasOwnProperty.call,这是为了避免出现键为hasOwnProperty.Yep的项时可能出现的错误。就这样。我可以肯定地说。干杯。结果[键]=结果[键]| |[]。按回车键;如果将result初始化为let result=Object.createnull,则可以省去hasOwnProperty测试,因为result将只具有自己的属性。和Object.prototype.hasOwnProperty.callresult,key似乎不必要的冗长,为什么不result.hasOwnPropertykey?@RobG我不想假设OP的其余程序能够处理空原型对象。对于Object.prototype.hasOwnProperty.call,这是为了避免出现键为hasOwnProperty的项时可能出现的错误。