如何从数组的键值对创建一个新数组?



我需要一种有效的方法来转换这个数组:

[
{
uuid: 1,
date: '2020-12-25',
note: 'example note 1',
time: '12:05:00',
language: ['english']
},
{
uuid: 2,
date: '2020-12-25',
note: 'example note 2',
time: '12:05:00',
language: ['french']
},
{
uuid: 3,
date: '2020-12-26',
note: 'example note 3',
time: '12:05:00',
language: ['spanish']
},
{
uuid: 4,
date: '2020-12-26',
note: 'example note 4',
time: '12:05:00',
language: ['chinese']
},
]

到这个按日期分组的新阵列

[
{
date:'2020-12-26',
details:[
{
uuid: 3,
date: '2020-12-26',
note: 'example note 3',
time: '12:05:00',
language: ['spanish']
},
{
uuid: 4,
date: '2020-12-26',
note: 'example note 4',
time: '12:05:00',
language: ['chinese']
},
]
},
{
date:'2020-12-25',
details:[
{
uuid: 1,
date: '2020-12-26',
note: 'example note 1',
time: '12:05:00',
language: ['english']
},
{
uuid: 2,
date: '2020-12-26',
note: 'example note 2',
time: '12:05:00',
language: ['french']
},
]
},
]

我尝试过使用lodash_.groupBy,但这不是我想要的数组外观,我尝试过将.filter、.reduce和.map组合在一起,但它真的很长,看起来根本不高效

我会使用.reduce().find()作为:

const data = [{ uuid: 1,date: '2020-12-25',note: 'example note 1',time: '12:05:00',language: ['english']},{uuid: 2,date: '2020-12-25',note: 'example note 2',time: '12:05:00',language: ['french']},{uuid: 3,date: '2020-12-26',note: 'example note 3',time: '12:05:00',language: ['spanish']}, {uuid: 4,date: '2020-12-26',note: 'example note 4',time: '12:05:00',language: ['chinese']},]
const result = data.reduce((a, c) => {
const found = a.find(e => e.date === c.date);
const elem = { uuid: c.uuid, note: c.note, time: c.time, language: c.language };
if (found) {
found.details.push(elem);
} else {
a.push({
date: c.date,
details: [elem]
});
}

return a;
}, []);
console.log(result)

您可以使用reduce函数创建组并将值分配给它们:

const input = [
{
uuid: 1,
date: '2020-12-25',
note: 'example note 1',
time: '12:05:00',
language: ['english']
},
{
uuid: 2,
date: '2020-12-25',
note: 'example note 2',
time: '12:05:00',
language: ['french']
},
{
uuid: 3,
date: '2020-12-26',
note: 'example note 3',
time: '12:05:00',
language: ['spanish']
},
{
uuid: 4,
date: '2020-12-26',
note: 'example note 4',
time: '12:05:00',
language: ['chinese']
},
];

const output = input.reduce((result, record) => {
if (!result[record.date]) result[record.date] = { date: record.date, details: [] };
result[record.date].details.push(record);

return result;
}, {});
console.log(output);
// If you want it as an array
console.log(Object.values(output));

我没有直接将其创建为数组,而是将其创建为由日期作为关键字的对象。这样我就不必在所有结果中搜索正确的结果了。

我先看看我是否有那个日期的条目。如果我不这样做,我就做一个。

然后我简单地将记录添加到该条目的详细信息中。

最后,如果您真的需要,我只需使用Object.values()就可以将它转换为数组。

Ciao,您可以先做一个lodashchain,然后再做一个groupBy,然后是一个map。像这样:

let input = [
{
uuid: 1,
date: '2020-12-25',
note: 'example note 1',
time: '12:05:00',
language: ['english']
},
{
uuid: 2,
date: '2020-12-25',
note: 'example note 2',
time: '12:05:00',
language: ['french']
},
{
uuid: 3,
date: '2020-12-26',
note: 'example note 3',
time: '12:05:00',
language: ['spanish']
},
{
uuid: 4,
date: '2020-12-26',
note: 'example note 4',
time: '12:05:00',
language: ['chinese']
},
]

console.log(
_.chain(input)
// Group the elements of Array based on `date` property
.groupBy("date")
.map((value, key) => ({ date: key, details: value }))
.value()
);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

这里有一个解决方案:https://jsfiddle.net/9p1fx3dq/1/

var a = [
{
uuid: 1,
date: '2020-12-25',
note: 'example note 1',
time: '12:05:00',
language: ['english']
},
{
uuid: 2,
date: '2020-12-25',
note: 'example note 2',
time: '12:05:00',
language: ['french']
},
{
uuid: 3,
date: '2020-12-26',
note: 'example note 3',
time: '12:05:00',
language: ['spanish']
},
{
uuid: 4,
date: '2020-12-26',
note: 'example note 4',
time: '12:05:00',
language: ['chinese']
},
];

function groupBy(key) {
return function group(array) {
return array.reduce((acc, obj) => {
const property = obj[key];
if (!acc.some(s => s.date === property)){
acc.push({date: property, details: []});
}
var details = acc.find(s => s.date === property).details;
details.push(obj);
return acc;
}, []);
};
}

console.log(groupBy('date')(a));

我使用new Set来获得不同的值

const array1 = [
{
uuid: 1,
date: "2020-12-25",
note: "example note 1",
time: "12:05:00",
language: ["english"]
},
{
uuid: 2,
date: "2020-12-25",
note: "example note 2",
time: "12:05:00",
language: ["french"]
},
{
uuid: 3,
date: "2020-12-26",
note: "example note 3",
time: "12:05:00",
language: ["spanish"]
},
{
uuid: 4,
date: "2020-12-26",
note: "example note 4",
time: "12:05:00",
language: ["chinese"]
}
];
const dates = array1.map(x => x.date);
const uniqueDates = [...new Set(dates)];
const result = uniqueDates.map(date => {
return {
date: date,
details: array1.filter(f => f.date === date)
}
});

最新更新