减少和嵌套数组

huangapple go评论95阅读模式
英文:

Reduce & Nested Array

问题

  1. "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:

  1. [
  2. {
  3. "pets": [
  4. {
  5. "name": "Hound",
  6. "id": "290dH8Z7nE3kKqMWcsj5"
  7. },
  8. {
  9. "name": "Roger",
  10. "id": "290dH8Z7nE3kKqMWcsj7"
  11. }
  12. ],
  13. "date": [
  14. {
  15. "date": "2023-05-11"
  16. }
  17. ],
  18. "typeofservice": "Service 1",
  19. "datestart": "2023-05-11",
  20. "outsideshift": false,
  21. "organizationId": "vrpMguv6cwf7L9KLEknR",
  22. "AddressLine1": "7j2UsMaCI1ehnHl5FbMf",
  23. }
  24. ]
  1. const partialDetails = a3.reduce((res, item) => {
  2. res.push({ Subject: item.typeofservice , StartTime: item.datestart, IsAllDay: true, CategoryColor: "#1aaa55", Description: "Address: " + item.AddressLine1 })
  3. return res;
  4. }, []);

Results Currently

  1. [
  2. {
  3. "Subject": "Service 1",
  4. "StartTime": "2023-05-11",
  5. "IsAllDay": true,
  6. "CategoryColor": "#1aaa55",
  7. "Description": "Address: AddressLine1Address"
  8. }
  9. ]

Wanted result the array in pets to be able to go into the subject line. There could be 1 there could be many.

  1. [
  2. {
  3. "Subject": "Service 1 - Hound, Roger",
  4. "StartTime": "2023-05-11",
  5. "IsAllDay": true,
  6. "CategoryColor": "#1aaa55",
  7. "Description": "Address: AddressLine1Address"
  8. }
  9. ]

Ive tried map inside but that creates a new line

答案1

得分: 1

这是您提供的代码的翻译部分:

  1. const a = [
  2. {
  3. "pets": [
  4. {
  5. "name": "Hound",
  6. "id": "290dH8Z7nE3kKqMWcsj5"
  7. },
  8. {
  9. "name": "Roger",
  10. "id": "290dH8Z7nE3kKqMWcsj7"
  11. }
  12. ],
  13. "date": [
  14. {
  15. "date": "2023-05-11"
  16. }
  17. ],
  18. "typeofservice": "Service 1",
  19. "datestart": "2023-05-11",
  20. "outsideshift": false,
  21. "organizationId": "vrpMguv6cwf7L9KLEknR",
  22. "AddressLine1": "7j2UsMaCI1ehnHl5FbMf",
  23. }
  24. ]
  25. const partialDetails = a.reduce((res, item) => {
  26. const subject = item.typeofservice + ' - '+ item.pets.map(pet=>pet.name).join( ', ');
  27. res.push({ Subject: subject , StartTime: item.datestart, IsAllDay: true, CategoryColor: "#1aaa55", Description: "Address: " + item.AddressLine1 })
  28. return res;
  29. }, []);
  30. console.log(partialDetails)

请注意,这只是提供了代码的翻译,不包含其他内容。

英文:

You can try:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const a = [
  2. {
  3. &quot;pets&quot;: [
  4. {
  5. &quot;name&quot;: &quot;Hound&quot;,
  6. &quot;id&quot;: &quot;290dH8Z7nE3kKqMWcsj5&quot;
  7. },
  8. {
  9. &quot;name&quot;: &quot;Roger&quot;,
  10. &quot;id&quot;: &quot;290dH8Z7nE3kKqMWcsj7&quot;
  11. }
  12. ],
  13. &quot;date&quot;: [
  14. {
  15. &quot;date&quot;: &quot;2023-05-11&quot;
  16. }
  17. ],
  18. &quot;typeofservice&quot;: &quot;Service 1&quot;,
  19. &quot;datestart&quot;: &quot;2023-05-11&quot;,
  20. &quot;outsideshift&quot;: false,
  21. &quot;organizationId&quot;: &quot;vrpMguv6cwf7L9KLEknR&quot;,
  22. &quot;AddressLine1&quot;: &quot;7j2UsMaCI1ehnHl5FbMf&quot;,
  23. }
  24. ]
  25. const partialDetails = a.reduce((res, item) =&gt; {
  26. const subject = item.typeofservice + &#39; - &#39;+ item.pets.map(pet=&gt;pet.name).join( &#39;, &#39;);
  27. res.push({ Subject: subject , StartTime: item.datestart, IsAllDay: true, CategoryColor: &quot;#1aaa55&quot;, Description: &quot;Address: &quot; + item.AddressLine1 })
  28. return res;
  29. }, []);
  30. 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;
}, []);

英文:
  1. const partialDetails = data.reduce((res, item) =&gt; {
  2. res.push({ Subject: `${item.typeofservice} - ${item.pets.map((el)=&gt;el.name).join(&#39;, &#39;)}` , StartTime: item.datestart, IsAllDay: true, CategoryColor: &quot;#1aaa55&quot;, Description: &quot;Address: &quot; + item.AddressLine1 })
  3. return res;
  4. }, []);

basically the same as previous answer using map and join for the names.

huangapple
  • 本文由 发表于 2023年5月10日 21:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定