减少和嵌套数组

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

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 = [
    {
        &quot;pets&quot;: [
            {
                &quot;name&quot;: &quot;Hound&quot;,
                &quot;id&quot;: &quot;290dH8Z7nE3kKqMWcsj5&quot;
            },

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

 const partialDetails = a.reduce((res, item) =&gt; {
 const subject = item.typeofservice + &#39; - &#39;+ item.pets.map(pet=&gt;pet.name).join( &#39;, &#39;);
 
  res.push({ Subject: subject , StartTime: item.datestart, IsAllDay: true, CategoryColor: &quot;#1aaa55&quot;, Description: &quot;Address: &quot; + 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) =&gt; {
    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 })
    return res;
  }, []);

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:

确定