Javascript 对数组项进行分组,但关键是数组
将数组项按其属性之一分组,问题是,该属性是数组 规则: 该属性中的每个项必须相同,才能在单个组中使用 示例阵列:Javascript 对数组项进行分组,但关键是数组,javascript,jquery,arrays,reactjs,group-by,Javascript,Jquery,Arrays,Reactjs,Group By,将数组项按其属性之一分组,问题是,该属性是数组 规则: 该属性中的每个项必须相同,才能在单个组中使用 示例阵列: [{name:'classOne',id:"c1",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"}]}, {name:'classTwo',id:"c2",student:[{name:'student1',id:"student1"}]}, {name:'classThree',i
[{name:'classOne',id:"c1",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"}]},
{name:'classTwo',id:"c2",student:[{name:'student1',id:"student1"}]},
{name:'classThree',id:"c3",student:[{name:'student3',id:"student3"}]},
{name:'classFour',id:"c4",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"}]},
{name:'classFive',id:"c4",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"},{name:'student3',id:"student3"}]}
]
预期产出:
{
key1:[
{name:'classOne',id:"c1",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"}]
{name:'classFour',id:"c4",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"}]
],
key2:[
{name:'classTwo',id:"c2",student:[{name:'student1',id:"student1"}]},
],
key3:[
{name:'classThree',id:"c3",student:[{name:'student3',id:"student3"}]},
],
key4:[
{name:'classFive',id:"c4",student:[{name:'student1',id:"student1"},{name:'student2',id:"student2"},{name:'student3',id:"student3"}]}
]
}
我的示例函数
export function groupByArrayKey<T>(key: string, arrayValue: T[]) {
let result = {};
return arrayValue.reduce((result, currentVal) => {
const currentArray = getDeepValue(currentVal, key);
const resultKey = currentArray[0].id;
const keyWithLen = resultKey + currentArray.length;
if (resultKey in result) {
const resultArray = getDeepValue(result[resultKey][0], key);
const isEql = isEqual(resultArray, currentArray);
if (isEql) {
(result[resultKey] = result[resultKey] || []).push(currentVal);
} else {
(result[keyWithLen] = result[keyWithLen] || []).push(currentVal);
}
} else if (keyWithLen in result) {
const resultArray = getDeepValue(result[keyWithLen][0], key);
const isEql = isEqual(resultArray, currentArray);
if (isEql) {
delete result[keyWithLen];
(result[resultKey] = result[resultKey] || []).push(resultArray, currentVal);
} else {
const dummyKey = Math.random();
(result[dummyKey] = result[dummyKey] || []).push(currentVal);
}
} else {
console.log('third');
(result[resultKey] = result[resultKey] || []).push(currentVal);
}
return result;
}, {});
}
导出函数groupByArrayKey(键:string,arrayValue:T[]){
让结果={};
返回arrayValue.reduce((结果,currentVal)=>{
const currentary=getDeepValue(currentVal,key);
const resultKey=currentArray[0]。id;
const keyWithLen=resultKey+currentArray.length;
如果(结果输入结果){
const resultArray=getDeepValue(结果[resultKey][0],键);
常量isEql=isEqual(结果数组,当前数组);
if(isEql){
(结果[resultKey]=结果[resultKey]| |[])。推送(currentVal);
}否则{
(结果[keyWithLen]=结果[keyWithLen]| |[])。推送(currentVal);
}
}else if(结果中的keyWithLen){
const resultArray=getDeepValue(结果[keyWithLen][0],键);
常量isEql=isEqual(结果数组,当前数组);
if(isEql){
删除结果[keyWithLen];
(结果[resultKey]=结果[resultKey]| |[])。推送(resultArray,currentVal);
}否则{
const dummyKey=Math.random();
(结果[dummyKey]=结果[dummyKey]| |[])。推送(currentVal);
}
}否则{
console.log('third');
(结果[resultKey]=结果[resultKey]| |[])。推送(currentVal);
}
返回结果;
}, {});
}
期望最终输出可以是“数组的数组”/“数组的对象”,一切都可以
团员:学生
下面的函数正在工作,但是,是否还有其他方法可以以紧凑的方式编写它?一个选项是创建一个属性为stringized
student
的对象。在输入的每一次迭代中,严格要求学生找到要查找的键。如果对象上不存在该键,请在那里创建一个数组。然后推到阵列
例如,{name:'name',student:['a',b']}
作为输入数组中的第一项的输入对象将导致该对象在第一次迭代时看起来像这样:
{
'["a","b"]': [{ name: 'name', student: ['a', 'b'] }]
}
最后,获取对象值:
const输入=[
{姓名:'classOne',id:'c1',学生:[{姓名:'student1',id:'student1},{姓名:'student2',id:'student2}]},
{姓名:'classTwo',id:'c2',学生:[{姓名:'student1',id:'student1}]},
{姓名:'classThree',id:'c3',学生:[{姓名:'student3',id:'student3}]},
{姓名:'classFour',id:'c4',学生:[{姓名:'student1',id:'student1},{姓名:'student2',id:'student2}]},
{姓名:'classFive',id:'c4',学生:[{姓名:'student1',id:'student1},{姓名:'student2',id:'student2},{姓名:'student3',id:'student3}]}
];
常量groupByArrayKey=(键,输入端)=>{
const outputarraysbystringfiedvalue={};
for(inputArr的常量项){
const key=JSON.stringify(item.student);
如果(!outputaraysbystringifiedvalue[key])outputaraysbystringifiedvalue[key]=[];
OutputArraySbyString-GifiedValue[键]。推送(项);
}
返回Object.values(outputarraysbystringfiedvalue);
}
日志(groupByArrayKey('student',input))代码>嘿,当然是性能,它真的很有效,谢谢。它可能以对象的形式获得最终输出(keyname不重要)。如果键真的不重要,那么您可以删除对象的一部分。values
以获得其键是数组而不是数组的对象。