本文介绍了Javascript按数组字段分组并减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数组.我想按 createdAt
分组.
这是我预期的结果.我希望之前的数组按 createdAt 分组,如果每个项目具有相同的 createdAt,则推送每个项目.
[{createdAtDates":[17-05-2021"]},{"createdAtDates: ["10-03-2021","10-03-2021"]},{createdAtDates":[13-04-2019"]},{createdAtDates":[01-05-2015"、01-05-2015"]}]这就是我所做的:
grouppedPallets() {返回 this.items.reduce(功能(托盘,托盘){const 组 = Pallets.filter((item) => item.date ===pallet.date);const found = groups.length === 1;const 组 = 找到?组[0]:{createdAt:pallet.date,createdAtDates:[]};group.createdAtDates.push(pallet.date);如果(!找到)托盘.推(组);返回托盘;}, []);},项目() {返回 this.palletsConnection?this.palletsConnection.values.map((pallet) => ({...托盘,date: formatDate(pallet.createdAt),//FORMAT DD-MM-YYYY})): [];},
这就是我的 grouppedPallet()
所做的:
我只想在减少数组后存储 createdAtDates
.
解决方案
您可以轻松地在 items
数组上实现第一张映射,并在 dd-mm-yyyy
中获取数据code> 使用正则表达式格式化,然后使用reduce来生成最终结果.
const items = [{createdAt: "2021-05-17T14:55:29.836Z",机器: {标签:MAQ_100",},},{createdAt: "2021-03-10T13:22:45.694Z",机器: {标签:MAQ_33",},},{createdAt: "2021-03-10T13:22:44.766Z",机器: {标签:MAQ_33",},},{createdAt: "2019-04-13T10:06:13.120Z",机器: {标签:MAQ_33",},},{createdAt: "2015-05-01T09:48:08.463Z",机器: {标签:MAQ_33",},},{createdAt: "2015-05-01T09:48:08.463Z",机器: {标签:MAQ_77",},},];const 结果 = 项目.map(({ createdAt }) => {返回 {createdAtDates: createdAt.split("T")[0].replace(/(\d{4})-(\d{2})-(\d{2})/, "$3-$2-$1"),};}).reduce((acc, curr) => {const { createdAtDates } = curr;const isExist = acc.find((o) => {返回 o.createdAtDates[0] === createdAtDates;});如果(isExist){isExist.createdAtDates.push(createdAtDates);} 别的 {acc.push({ createdAtDates: [createdAtDates] });}返回acc;}, []);console.log(result);
I have this array. I want to group by createdAt
.
[
{
"createdAt": "2021-05-17T14:55:29.836Z",
"machine": {
"label": "MAQ_100",
},
},
{
"createdAt": "2021-03-10T13:22:45.694Z",
"machine": {
"label": "MAQ_33",
},
},
{
"createdAt": "2021-03-10T13:22:44.766Z",
"machine": {
"label": "MAQ_33",
},
},
{
"createdAt": "2019-04-13T10:06:13.120Z",
"machine": {
"label": "MAQ_33",
},
},
{
"createdAt": "2015-05-01T09:48:08.463Z",
"machine": {
"label": "MAQ_33",
},
},
{
"createdAt": "2015-05-01T09:48:08.463Z",
"machine": {
"label": "MAQ_77",
},
}
],
This is my expected result. I want the previous array grouped by createdAt and push each item if it has the same createdAt.
[
{"createdAtDates": ["17-05-2021"]},
{"createdAtDates: ["10-03-2021","10-03-2021"]},
{"createdAtDates": ["13-04-2019"]},
{"createdAtDates": ["01-05-2015","01-05-2015"]}
]
This is what I have done:
grouppedPallets() {
return this.items.reduce(function (pallets, pallet) {
const groups = pallets.filter((item) => item.date === pallet.date);
const found = groups.length === 1;
const group = found ? groups[0] : { createdAt: pallet.date, createdAtDates: [] };
group.createdAtDates.push(pallet.date);
if (!found) pallets.push(group);
return pallets;
}, []);
},
items() {
return this.palletsConnection
?
this.palletsConnection.values.map((pallet) => ({
...pallet,
date: formatDate(pallet.createdAt), //FORMAT DD-MM-YYYY
}))
: [];
},
This is what my grouppedPallet()
does:
[
{
"createdAt": "17-05-2021",
"createdAtDates": ["17-05-2021"]
},
{
"createdAt": "10-03-2021",
"createdAtDates": ["10-03-2021","10-03-2021"]
},
{
"createdAt": "13-04-2019",
"createdAtDates": ["13-04-2019"]
},
{
"createdAt": "01-05-2015",
"createdAtDates":["01-05-2015","01-05-2015"]
}
]
I only want to store the createdAtDates
after having reduced the array.
解决方案
You can easily achieve this first map over the items
array and get the data in dd-mm-yyyy
format using regex and then using reduce to make the final result.
const items = [
{
createdAt: "2021-05-17T14:55:29.836Z",
machine: {
label: "MAQ_100",
},
},
{
createdAt: "2021-03-10T13:22:45.694Z",
machine: {
label: "MAQ_33",
},
},
{
createdAt: "2021-03-10T13:22:44.766Z",
machine: {
label: "MAQ_33",
},
},
{
createdAt: "2019-04-13T10:06:13.120Z",
machine: {
label: "MAQ_33",
},
},
{
createdAt: "2015-05-01T09:48:08.463Z",
machine: {
label: "MAQ_33",
},
},
{
createdAt: "2015-05-01T09:48:08.463Z",
machine: {
label: "MAQ_77",
},
},
];
const result = items
.map(({ createdAt }) => {
return {
createdAtDates: createdAt
.split("T")[0]
.replace(/(\d{4})-(\d{2})-(\d{2})/, "$3-$2-$1"),
};
})
.reduce((acc, curr) => {
const { createdAtDates } = curr;
const isExist = acc.find((o) => {
return o.createdAtDates[0] === createdAtDates;
});
if (isExist) {
isExist.createdAtDates.push(createdAtDates);
} else {
acc.push({ createdAtDates: [createdAtDates] });
}
return acc;
}, []);
console.log(result);
这篇关于Javascript按数组字段分组并减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!