英文:
Reduce & Nested Array
问题
"Subject": "Service 1 - Hound, Roger",
英文:
I am merging two arrays which is working great. But i am showing the data on a calendar and i need an array to be used as a single line.
My Data:
[
{
"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;
}, []);
Results Currently
[
{
"Subject": "Service 1",
"StartTime": "2023-05-11",
"IsAllDay": true,
"CategoryColor": "#1aaa55",
"Description": "Address: AddressLine1Address"
}
]
Wanted result the array in pets to be able to go into the subject line. There could be 1 there could be many.
[
{
"Subject": "Service 1 - Hound, Roger",
"StartTime": "2023-05-11",
"IsAllDay": true,
"CategoryColor": "#1aaa55",
"Description": "Address: AddressLine1Address"
}
]
Ive tried map inside but that creates a new line
答案1
得分: 1
这是您提供的代码的翻译部分:
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)
请注意,这只是提供了代码的翻译,不包含其他内容。
英文:
You can try:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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)
<!-- end snippet -->
答案2
得分: 0
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;
}, []);
英文:
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;
}, []);
basically the same as previous answer using map and join for the names.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论