Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何按多个字段对值进行分组并按特定类型进行排序_Javascript_Functional Programming - Fatal编程技术网

Javascript 如何按多个字段对值进行分组并按特定类型进行排序

Javascript 如何按多个字段对值进行分组并按特定类型进行排序,javascript,functional-programming,Javascript,Functional Programming,我试图根据特定字段(值)对对象数组进行分组,然后根据对象的特定类型对其进行排序: var列表=[ {值:'fox',国家:'nl',类型:'animal',优先级:1}, {值:'fox',国家:'be',类型:'animal',优先级:2}, {值:'cat',国家:'de',类型:'animal',优先级:3}, {值:'david',国家:'nl',类型:'human',优先级:4}, {值:'marc',国家:'be',类型:'human',优先级:5}, {值:'lola',国家:'d

我试图根据特定字段(值)对对象数组进行分组,然后根据对象的特定类型对其进行排序:

var列表=[
{值:'fox',国家:'nl',类型:'animal',优先级:1},
{值:'fox',国家:'be',类型:'animal',优先级:2},
{值:'cat',国家:'de',类型:'animal',优先级:3},
{值:'david',国家:'nl',类型:'human',优先级:4},
{值:'marc',国家:'be',类型:'human',优先级:5},
{值:'lola',国家:'de',类型:'human',优先级:6},
{值:'tiger',国家:'nl',类型:'animal',优先级:7},
{值:'koala',国家:'be',类型:'animal',优先级:8},
{值:'tiger',国家:'nl',类型:'animal',优先级:9},
];
//输出/结果应为:
[
{值:'fox',国家:['nl','be'],类型:'animal',优先级:1},
{值:'cat',国家:['de'],类型:'animal',优先级:2},
{值:'tiger',国家:['nl'],类型:'animal',优先级:3},
{值:'koala',图案:['be'],类型:'animal',优先级:4},
{value:'david',contries:['nl'],类型:'human',优先级:1},
{值:'marc',配置:['be'],类型:'human',优先级:2},
{value:'lola',contries:['de'],type:'human',优先级:3},
];
我正试图用一个减速机来删除重复的,但我无法将国家分组

list.reduce((累加器、当前值、索引)=>{
const{country,value}=currentValue;
如果(!acculator.some(item=>item.value==currentValue.value)){
推({
值:currentValue.value,
国家:[累加器.country].concat(currentValue.country)
})
}
回流蓄能器;
}, [])

您需要检查该值的现有值(如果不存在),然后添加它,否则将新值推送到国家/地区数组

var列表=[
{值:'fox',国家:'nl',类型:'animal',优先级:1},
{值:'fox',国家:'be',类型:'animal',优先级:2},
{值:'cat',国家:'de',类型:'animal',优先级:3},
{值:'david',国家:'nl',类型:'human',优先级:4},
{值:'marc',国家:'be',类型:'human',优先级:5},
{值:'lola',国家:'de',类型:'human',优先级:6},
{值:'tiger',国家:'nl',类型:'animal',优先级:7},
{值:'koala',国家:'be',类型:'animal',优先级:8},
{值:'tiger',国家:'nl',类型:'animal',优先级:9},
];
const l2=list.reduce((累加器、当前值、索引)=>{
常数{
国家,,
价值
}=当前值;
常数=累加器
.find(item=>item.value==currentValue.value);
如果(现有){
常数{
国家
}=现有;
国家。推动(国家);
existing.countries=Array.from(新集合(国家))
}否则{
累加器.push({…值,
国家:[国家]
});
}
回流蓄能器;
}, [])
console.log(l2)
。作为控制台包装器{
最大高度:100%!重要;
排名:0;

}
您需要检查该值的现有值(如果不存在),然后添加它,否则将新值推送到国家/地区数组

var列表=[
{值:'fox',国家:'nl',类型:'animal',优先级:1},
{值:'fox',国家:'be',类型:'animal',优先级:2},
{值:'cat',国家:'de',类型:'animal',优先级:3},
{值:'david',国家:'nl',类型:'human',优先级:4},
{值:'marc',国家:'be',类型:'human',优先级:5},
{值:'lola',国家:'de',类型:'human',优先级:6},
{值:'tiger',国家:'nl',类型:'animal',优先级:7},
{值:'koala',国家:'be',类型:'animal',优先级:8},
{值:'tiger',国家:'nl',类型:'animal',优先级:9},
];
const l2=list.reduce((累加器、当前值、索引)=>{
常数{
国家,,
价值
}=当前值;
常数=累加器
.find(item=>item.value==currentValue.value);
如果(现有){
常数{
国家
}=现有;
国家。推动(国家);
existing.countries=Array.from(新集合(国家))
}否则{
累加器.push({…值,
国家:[国家]
});
}
回流蓄能器;
}, [])
console.log(l2)
。作为控制台包装器{
最大高度:100%!重要;
排名:0;
}
你需要三次传球

  • 按所需键和值分组
  • 对数组进行排序
  • 更新
    priorityey
    属性
函数分组依据(数据、键、…值){
返回Object.values(data.reduce((r,o)=>{
r[o[key]]=r[o[key]|{…o,…Object.fromEntries(values.map(k=>[k,[]))};
forEach(k=>r[o[key][k].push(o[k]);
返回r;
}, {}));
}
var list=[{value:'fox',country:'nl',type:'animal',priority:1},{value:'fox',country:'be',type:'animal',priority:2},{value:'cat',country:'de',type:'animal',priority:3},{value:'david country:'nl type:'human',priority:4},{value:'marc',country:'be',type:'human',priority:5},{value:'lola',country:'de',type:'human',priority:6},{value:'tiger',country:'nl',type:'animal',priority:7},{value:'koala',country:'be',type:'animal',priority:8},{value:'tiger',country:'nl type:'animal priority:9},
结果=分组依据(列表“值”、“国家”)
.sort({type:a},{type:b})=>a>b | |-(a(o,i,{[i-1]:p})=>{
如果(p&p.type===o.type)+优先级;
else优先级=1;
返回{…o,优先级};
})());
console.log(结果);
。作为控制台包装{max height:100%!important;top:0;}
您需要三次通过

  • 通缉组