javascript 减少嵌套数组(& N)

js4nwp54  于 2023-05-12  发布在  Java
关注(0)|答案(3)|浏览(86)

我正在合并两个数组,它工作得很好。但我在日历上显示的数据,我需要一个数组作为一个单一的行使用。
我的数据:

[
    {
        "pets": [
            {
                "name": "Hound",
                "id": "290dH8Z7nE3kKqMWcsj5"
            },

            {
                "name": "Roger",
                "id": "290dH8Z7nE3kKqMWcsj7"
            }
        ],
        "date": [
            {
                "date": "2023-05-11"
            }
        ],
        "typeofservice": "Service 1",
        "datestart": "2023-05-11",
        "outsideshift": false,
        "organizationId": "vrpMguv6cwf7L9KLEknR",
        "AddressLine1": "7j2UsMaCI1ehnHl5FbMf",
    }
]
const partialDetails = a3.reduce((res, item) => {
  res.push({ Subject: item.typeofservice , StartTime: item.datestart, IsAllDay: true, CategoryColor: "#1aaa55", Description: "Address: " + item.AddressLine1 })
  return res;
}, []);

目前的结果

[
    {
        "Subject": "Service 1",
        "StartTime": "2023-05-11",
        "IsAllDay": true,
        "CategoryColor": "#1aaa55",
        "Description": "Address: AddressLine1Address"
    }
]

想要的结果在宠物数组中可以进入主题行。可能有一个,也可能有很多。

[
    {
        "Subject": "Service 1 - Hound, Roger",
        "StartTime": "2023-05-11",
        "IsAllDay": true,
        "CategoryColor": "#1aaa55",
        "Description": "Address: AddressLine1Address"
    }
]

我尝试了Map里面,但创建了一个新的行

scyqe7ek

scyqe7ek1#

您可以尝试:

const a = [
    {
        "pets": [
            {
                "name": "Hound",
                "id": "290dH8Z7nE3kKqMWcsj5"
            },

            {
                "name": "Roger",
                "id": "290dH8Z7nE3kKqMWcsj7"
            }
        ],
        "date": [
            {
                "date": "2023-05-11"
            }
        ],
        "typeofservice": "Service 1",
        "datestart": "2023-05-11",
        "outsideshift": false,
        "organizationId": "vrpMguv6cwf7L9KLEknR",
        "AddressLine1": "7j2UsMaCI1ehnHl5FbMf",
    }
]

 const partialDetails = a.reduce((res, item) => {
 const subject = item.typeofservice + ' - '+ item.pets.map(pet=>pet.name).join( ', ');
 
  res.push({ Subject: subject , StartTime: item.datestart, IsAllDay: true, CategoryColor: "#1aaa55", Description: "Address: " + item.AddressLine1 })
  return res;
}, []);

console.log(partialDetails)
8ulbf1ek

8ulbf1ek2#

const partialDetails = data.reduce((res, item) => {
    res.push({ Subject: `${item.typeofservice} - ${item.pets.map((el)=>el.name).join(', ')}` , StartTime: item.datestart, IsAllDay: true, CategoryColor: "#1aaa55", Description: "Address: " + item.AddressLine1 })
    return res;
  }, []);

基本上与前面的答案相同,使用map和join作为名称。

yyhrrdl8

yyhrrdl83#

ChatGPT帮我修好了-

const a4 = a3.reduce((accumulator, currentValue) => {
  const petNames = currentValue.pets.map((pet) => `${pet.name}`);
  const separator = petNames.length === 1 ? "" : ", ";
  const petData = {
  
      datestart: currentValue.datestart,
      typeofservice: `${currentValue.typeofservice} - ${petNames.join(separator)}`
  };
  if (!accumulator.some((item) => item.typeofservice === petData.typeofservice)) {
      accumulator.push(petData);
  }
  return accumulator;
}, []);

相关问题