我正在寻找一种排序大型数据集的方法,该方法类似于我在下面提供的方法,而无需使用大型循环。我正在寻找将“ 439190”的数据排序为多个数组,每个数组中仅包含“ colorid”之一。我一直在浏览所有的过滤,映射和归约方法,但没有成功,如果有人能指出正确的方向,那将是很好的。
原始Json输出:
{ '439190':
[ { assetid: '9354886749',
classid: 439190,
colorid: '1650467837',
category: 'student',
favouritecolor: '439190-Red' },
{ assetid: '9354886743',
classid: 439190,
colorid: '1650467837',
category: 'student',
favouritecolor: '439190-Red' },
{ assetid: '9354886777',
classid: 439190,
colorid: '1650467837',
category: 'student',
favouritecolor: '439190-Red' },
{ assetid: '9354886726',
classid: 439190,
colorid: '1650746832',
category: 'student',
favouritecolor: '439190-Blue' },
{ assetid: '9354886755',
classid: 439190,
colorid: '1650746832',
category: 'student',
favouritecolor: '439190-Blue' },
{ assetid: '9354886753',
classid: 439190,
colorid: '1650746832',
category: 'student',
favouritecolor: '439190-Blue' },
{ assetid: '9354869964',
classid: 439190,
colorid: '1649236744',
category: 'student',
favouritecolor: '439190-Green' },
{ assetid: '9354869970',
classid: 439190,
colorid: '1649236744',
category: 'student',
favouritecolor: '439190-Green' },
{ assetid: '9354869963',
classid: 439190,
colorid: '1649236744',
category: 'student',
favouritecolor: '439190-Green' },
{ assetid: '9354886738',
classid: 439190,
colorid: '1651234125',
category: 'student',
favouritecolor: '439190-Yellow' },
{ assetid: '9354886773',
classid: 439190,
colorid: '1651234125',
category: 'student',
favouritecolor: '439190-Yellow' },
{ assetid: '9354886739',
classid: 439190,
colorid: '1651234125',
category: 'student',
favouritecolor: '439190-Yellow' },
{ assetid: '9354869966',
classid: 439190,
colorid: '1651726369',
category: 'student',
favouritecolor: '439190-Pink' },
{ assetid: '9354886767',
classid: 439190,
colorid: '1651726369',
category: 'student',
favouritecolor: '439190-Pink' },
{ assetid: '9354886727',
classid: 439190,
colorid: '1651726369',
category: 'student',
favouritecolor: '439190-Pink' },
{ assetid: '9354886763',
classid: 439190,
colorid: '1642929857',
category: 'student',
favouritecolor: '439190-Orange' },
{ assetid: '9354886722',
classid: 439190,
colorid: '1642929857',
category: 'student',
favouritecolor: '439190-Orange' },
{ assetid: '9354886762',
classid: 439190,
colorid: '1642929857',
category: 'student',
favouritecolor: '439190-Orange' } ],
预期的输出(大致但希望您能理解):
{ '439190':
[ [ { assetid: '9354886749',
classid: 439190,
colorid: '1650467837',
category: 'student',
favouritecolor: '439190-Red' },
{ assetid: '9354886726',
classid: 439190,
colorid: '1650746832',
category: 'student',
favouritecolor: '439190-Blue' },
{ assetid: '9354869964',
classid: 439190,
colorid: '1649236744',
category: 'student',
favouritecolor: '439190-Green' },
{ assetid: '9354886738',
classid: 439190,
colorid: '1651234125',
category: 'student',
favouritecolor: '439190-Yellow' },
{ assetid: '9354869966',
classid: 439190,
colorid: '1651726369',
category: 'student',
favouritecolor: '439190-Pink' },
{ assetid: '9354886763',
classid: 439190,
colorid: '1642929857',
category: 'student',
favouritecolor: '439190-Orange' } ],
[ { assetid: '9354886743',
classid: 439190,
colorid: '1650467837',
category: 'student',
favouritecolor: '439190-Red' },
{ assetid: '9354886755',
classid: 439190,
colorid: '1650746832',
category: 'student',
favouritecolor: '439190-Blue' },
{ assetid: '9354869970',
classid: 439190,
colorid: '1649236744',
category: 'student',
favouritecolor: '439190-Green' },
{ assetid: '9354886773',
classid: 439190,
colorid: '1651234125',
category: 'student',
favouritecolor: '439190-Yellow' },
{ assetid: '9354886767',
classid: 439190,
colorid: '1651726369',
category: 'student',
favouritecolor: '439190-Pink' },
{ assetid: '9354886722',
classid: 439190,
colorid: '1642929857',
category: 'student',
favouritecolor: '439190-Orange' } ],
[ { assetid: '9354886777',
classid: 439190,
colorid: '1650467837',
category: 'student',
favouritecolor: '439190-Red' },
{ assetid: '9354886753',
classid: 439190,
colorid: '1650746832',
category: 'student',
favouritecolor: '439190-Blue' },
{ assetid: '9354869963',
classid: 439190,
colorid: '1649236744',
category: 'student',
favouritecolor: '439190-Green' },
{ assetid: '9354886739',
classid: 439190,
colorid: '1651234125',
category: 'student',
favouritecolor: '439190-Yellow' },
{ assetid: '9354886727',
classid: 439190,
colorid: '1651726369',
category: 'student',
favouritecolor: '439190-Pink' },
{ assetid: '9354886762',
classid: 439190,
colorid: '1642929857',
category: 'student',
favouritecolor: '439190-Orange' } ] ],
我当前的代码:
for(i in json){
test = Array.from(new Set(json[i].map(item => item.colorid)))
.map( colorid=> {
return {
"assetid":json[i].find(s => s.colorid=== colorid).assetid,
"classid":json[i].find(s => s.colorid=== colorid).classid,
"colorid":colorid,
"category":json[i].find(s => s.colorid=== colorid).category,
"favouritecolor":json[i].find(s => s.colorid=== colorid).favouritecolor
};
});
console.log(test);
}
它确实将结果过滤为仅包含唯一的颜色,并且保留所有关键数据,尽管它仅返回一个数组。至此,我一直没有找到有效的方法(不使用多个嵌入式循环)来获得上面显示的预期输出。任何改进我的代码或为我指明正确方向的帮助将不胜感激:)
答案 0 :(得分:2)
我只是将其组合在一起以给您一些指示,但是类似的东西应该会有所帮助。您需要创建一个reduce函数,该函数具有一个数组数组作为累加器。然后检查每个数组以查看是否存在匹配的元素,并将其添加到不存在的第一个数组中。如果每个数组中都存在该元素,则推一个新数组。
const array = [1, 2, 3, 4, 6, 4, 5, 6, 4];
const listReducer = (accumulator, currentValue) => {
let pushed = false;
accumulator.forEach((list) => {
if (!list.includes(currentValue)) {
list.push(currentValue);
pushed = true;
}
});
if (!pushed) {
accumulator.push([currentValue]);
}
return accumulator;
}
const startingValue = [];
console.log(array.reduce(listReducer, startingValue));