ruby 使用map do重构具有嵌套数组的对象数组

8yoxcaq7  于 11个月前  发布在  Ruby
关注(0)|答案(1)|浏览(108)

我使用下面的代码尝试重新Map“usageByMonth”数组,它位于多个对象的数组中。

categorisedFeatures = response[:categorisedFeatures].map do |feature|
   newHistorical = (feature.dig(1, :historical)).map do |historical|
     usageByMonthRefactor = historical.dig(:usageByMonth)&.map { |(k, v)| { 'month': k, 'value': v } }
           newEntry = historical.merge({
               usageByMonth: usageByMonthRefactor
            })
     end
    newFeature = feature.dig(1).merge ({
      historical: newHistorical
    }) 
 feature&.map { |(k,v)| k=k , v=newFeature}

 end

字符串
问题是,输出重复相同的“历史”对象多次,我不知道为什么。
下面是其中一个“feature”元素的输出示例:
CMS {:historical=>[{:internalName=>"TOTAL_BLOG_POSTS_LIVE_EOM", :definition=>"Blogs live", :toolTip=>"Number of blogs with a "published" status at the end of the report month.", :tags=>[], :type=>"NUMBER", :name=>"Blogs", :usageByMonth=>[{:month=>:SEP, :value=>"741"}, {:month=>:OCT, :value=>"741"}, {:month=>:AUG, :value=>"741"}]}, {:internalName=>"BLOG_POST_VIEWS", :definition=>"Page views", :toolTip=>:null, :tags=>[], :type=>"NUMBER", :name=>"Blogs", :usageByMonth=>[{:month=>:SEP, :value=>"6651"}, {:month=>:OCT, :value=>"6601"}, {:month=>:AUG, :value=>"6957"}]}, {:internalName=>"LANDING_PAGE_VIEWS", :definition=>"Page views", :toolTip=>:null, :tags=>[], :type=>"NUMBER", :name=>"Landing Pages", :usageByMonth=>[{:month=>:SEP, :value=>"68"}, {:month=>:OCT, :value=>"163"}, {:month=>:AUG, :value=>"116"}]}, {:internalName=>"WEBSITE_PAGE_VIEWS", :definition=>"Page views", :toolTip=>:null, :tags=>[], :type=>"NUMBER", :name=>"Website Pages", :usageByMonth=>[{:month=>:SEP, :value=>"1041"}, {:month=>:OCT, :value=>"979"}, {:month=>:AUG, :value=>"1268"}]}], :realtime=>[]} {:historical=>[{:internalName=>"TOTAL_BLOG_POSTS_LIVE_EOM", :definition=>"Blogs live", :toolTip=>"Number of blogs with a "published" status at the end of the report month.", :tags=>[], :type=>"NUMBER", :name=>"Blogs", :usageByMonth=>{:SEP=>"741", :OCT=>"741", :AUG=>"741"}}, {:internalName=>"BLOG_POST_VIEWS", :definition=>"Page views", :toolTip=>:null, :tags=>[], :type=>"NUMBER", :name=>"Blogs", :usageByMonth=>{:SEP=>"6651", :OCT=>"6601", :AUG=>"6957"}}, {:internalName=>"LANDING_PAGE_VIEWS", :definition=>"Page views", :toolTip=>:null, :tags=>[], :type=>"NUMBER", :name=>"Landing Pages", :usageByMonth=>{:SEP=>"68", :OCT=>"163", :AUG=>"116"}}, {:internalName=>"WEBSITE_PAGE_VIEWS", :definition=>"Page views", :toolTip=>:null, :tags=>[], :type=>"NUMBER", :name=>"Website Pages", :usageByMonth=>{:SEP=>"1041", :OCT=>"979", :AUG=>"1268"}}], :realtime=>[]} {:historical=>[{:internalName=>"TOTAL_BLOG_POSTS_LIVE_EOM", :definition=>"Blogs live", :toolTip=>"Number of blogs with a "published" status at the end of the report month.", :tags=>[], :type=>"NUMBER", :name=>"Blogs", :usageByMonth=>[{:month=>:SEP, :value=>"741"}, {:month=>:OCT, :value=>"741"}, {:month=>:AUG, :value=>"741"}]}, {:internalName=>"BLOG_POST_VIEWS", :definition=>"Page views", :toolTip=>:null, :tags=>[], :type=>"NUMBER", :name=>"Blogs", :usageByMonth=>[{:month=>:SEP, :value=>"6651"}, {:month=>:OCT, :value=>"6601"}, {:month=>:AUG, :value=>"6957"}]}, {:internalName=>"LANDING_PAGE_VIEWS", :definition=>"Page views", :toolTip=>:null, :tags=>[], :type=>"NUMBER", :name=>"Landing Pages", :usageByMonth=>[{:month=>:SEP, :value=>"68"}, {:month=>:OCT, :value=>"163"}, {:month=>:AUG, :value=>"116"}]}, {:internalName=>"WEBSITE_PAGE_VIEWS", :definition=>"Page views", :toolTip=>:null, :tags=>[], :type=>"NUMBER", :name=>"Website Pages", :usageByMonth=>[{:month=>:SEP, :value=>"1041"}, {:month=>:OCT, :value=>"979"}, {:month=>:AUG, :value=>"1268"}]}], :realtime=>[]}
以下是我的原始输入:

response = {
  "months": [
    "AUG",
    "SEP",
    "OCT"
  ],
  "categorisedTiers": {
    "CMS": "FREE",
    "MARKETING": "FREE",
    "OPERATIONS": "FREE",
    "SALES": "FREE",
    "SERVICE": "FREE"
  },
  "categorisedFeatures": {
    "PLATFORM": {
      "historical": [
        {
          "internalName": "TOTAL_CONTACTS_CREATED_IN_MONTH",
          "definition": "New contacts",
          "toolTip": "Number of contacts that were created in the report month.",
          "tags": [],
          "type": "NUMBER",
          "name": "Contacts",
          "usageByMonth": {
            "SEP": "33",
            "OCT": "35",
            "AUG": "40"
          }
        },
        {
          "internalName": "TOTAL_CONTACTS_QUALIFIED_IN_MONTH",
          "definition": "MQLs",
          "toolTip": "Number of contacts that were set with internal value 'marketingqualifiedlead' in the 'lifecycle stage' property in the report month.",
          "tags": [],
          "type": "NUMBER",
          "name": "Contacts",
          "usageByMonth": {
            "SEP": "0",
            "OCT": "0",
            "AUG": "0"
          }
        },
        {
          "internalName": "TOTAL_CONTACTS_BECAME_CUSTOMERS_IN_MONTH",
          "definition": "Customers",
          "toolTip": "Number of contacts that were set with internal value 'customer' in the 'lifecycle stage' property in the report month.",
          "tags": [],
          "type": "NUMBER",
          "name": "Contacts",
          "usageByMonth": {
            "SEP": "0",
            "OCT": "0",
            "AUG": "0"
          }
        },
        {
          "internalName": "CONTACTS_SCORED",
          "definition": "Contacts scored",
          "toolTip": "Overall percentage of all contacts with the 'HubSpot score' not equal to 0, in the report month.",
          "tags": [
            "PRO+"
          ],
          "type": "PERCENTAGE",
          "name": "Contacts",
          "usageByMonth": {
            "SEP": "25.09",
            "OCT": "25.05",
            "AUG": "25.11"
          }
        },
        {
          "internalName": "PERCENTAGE_CONTACTS_ASSIGNED",
          "definition": "Contacts assigned",
          "toolTip": "Overall percentage of contacts with an owner.",
          "tags": [],
          "type": "PERCENTAGE",
          "name": "Contacts",
          "usageByMonth": {
            "SEP": "19.67",
            "OCT": "19.65",
            "AUG": "19.67"
          }
        },
        {
          "internalName": "TOTAL_CONTACTS_CONTACTED_IN_MONTH",
          "definition": "Contacts contacted",
          "toolTip": "Number of contacts that were contacted through a call, chat conversation, LinkedIn message, postal mail, meeting, sales email, SMS, or WhatsApp message in the report month.",
          "tags": [],
          "type": "NUMBER",
          "name": "Contacts",
          "usageByMonth": {
            "SEP": "3",
            "OCT": "3",
            "AUG": "5"
          }
        },
        {
          "internalName": "TOTAL_CONVERSATIONS_STARTED_IN_MONTH",
          "definition": "Conversations started",
          "toolTip": "Number of conversations started, via any channel, in the report month (e.g. emails, chats, FaceBook Messanger, etc.)",
          "tags": [],
          "type": "NUMBER",
          "name": "Conversations",
          "usageByMonth": {
            "SEP": "0",
            "OCT": "0",
            "AUG": "0"
          }
        },
        {
          "internalName": "TOTAL_CUSTOM_REPORTS",
          "definition": "Custom reports",
          "toolTip": "Total number of custom reports as of the end of the report month.",
          "tags": [],
          "type": "NUMBER",
          "name": "Reporting",
          "usageByMonth": {
            "AUG": "27",
            "SEP": "27",
            "OCT": "27"
          }
        },
        {
          "internalName": "TOTAL_DASHBOARDS",
          "definition": "Dashboards",
          "toolTip": "Total number of dashboards as of the end of the report month.",
          "tags": [],
          "type": "NUMBER",
          "name": "Reporting",
          "usageByMonth": {
            "AUG": "4",
            "SEP": "4",
            "OCT": "4"
          }
        },
        {
          "internalName": "TOTAL_TEMPLATE_EMAIL_SENDS_THIS_MONTH",
          "definition": "Templates sent",
          "toolTip": :null,
          "tags": [],
          "type": "NUMBER",
          "name": "Templates",
          "usageByMonth": {
            "SEP": "0",
            "OCT": "0",
            "AUG": "0"
          }
        },
        {
          "internalName": "TOTAL_PAGE_VIEWS",
          "definition": "Page views",
          "toolTip": "Number of page views recorded in the report month. A page view is counted every time the HubSpot tracking code is loaded on a browser.",
          "tags": [],
          "type": "NUMBER",
          "name": "Traffic Analytics",
          "usageByMonth": {
            "SEP": "13522",
            "OCT": "12599",
            "AUG": "18599"
          }
        },
        {
          "internalName": "TOTAL_WORKFLOWS_CREATED_IN_MONTH",
          "definition": "Workflows created",
          "toolTip": :null,
          "tags": [
            "PRO+"
          ],
          "type": "NUMBER",
          "name": "Workflows",
          "usageByMonth": {
            "SEP": "0",
            "OCT": "1",
            "AUG": "0"
          }
        }
      ],
      "realtime": [
        {
          "internalName": "TOTAL_CUSTOM_OBJECTS",
          "definition": "Active custom objects",
          "toolTip": "Total number of active custom objects. This data is real time.",
          "tags": [
            "ENTERPRISE"
          ],
          "type": "NUMBER",
          "name": "Custom Objects",
          "realtimeValue": "0"
        }
      ]
    },
    "OPERATIONS": {
      "historical": [
        {
          "internalName": "DUPLICATE_RECORDS",
          "definition": "Duplicate records",
          "toolTip": "Count of duplicate records at end of report month.",
          "tags": [
            "PRO+"
          ],
          "type": "NUMBER",
          "name": "Data Quality",
          "usageByMonth": {
            "AUG": "0",
            "SEP": "0",
            "OCT": "0"
          }
        },
        {
          "internalName": "TOTAL_PROGRAMMABLE_ACTION_EVENTS",
          "definition": "Programmable actions",
          "toolTip": :null,
          "tags": [
            "PRO+"
          ],
          "type": "NUMBER",
          "name": "Workflows",
          "usageByMonth": {
            "SEP": "0",
            "OCT": "0",
            "AUG": "0"
          }
        }
      ],
      "realtime": [
        {
          "internalName": "DATASETS_CREATED",
          "definition": "Datasets live",
          "toolTip": "Number of datasets at the end of the report month. This data is real time.",
          "tags": [
            "ENTERPRISE"
          ],
          "type": "NUMBER",
          "name": "Dataset",
          "realtimeValue": "0"
        }
      ]
    },
    "CMS": {
      "historical": [
        {
          "internalName": "TOTAL_BLOG_POSTS_LIVE_EOM",
          "definition": "Blogs live",
          "toolTip": "Number of blogs with a \"published\" status at the end of the report month.",
          "tags": [],
          "type": "NUMBER",
          "name": "Blogs",
          "usageByMonth": {
            "SEP": "741",
            "OCT": "741",
            "AUG": "741"
          }
        },
        {
          "internalName": "BLOG_POST_VIEWS",
          "definition": "Page views",
          "toolTip": :null,
          "tags": [],
          "type": "NUMBER",
          "name": "Blogs",
          "usageByMonth": {
            "SEP": "6651",
            "OCT": "6601",
            "AUG": "6957"
          }
        },
        {
          "internalName": "LANDING_PAGE_VIEWS",
          "definition": "Page views",
          "toolTip": :null,
          "tags": [],
          "type": "NUMBER",
          "name": "Landing Pages",
          "usageByMonth": {
            "SEP": "68",
            "OCT": "163",
            "AUG": "116"
          }
        },
        {
          "internalName": "WEBSITE_PAGE_VIEWS",
          "definition": "Page views",
          "toolTip": :null,
          "tags": [],
          "type": "NUMBER",
          "name": "Website Pages",
          "usageByMonth": {
            "SEP": "1041",
            "OCT": "979",
            "AUG": "1268"
          }
        }
      ],
      "realtime": []
    }
    
  }
}


任何帮助是非常感谢!
我尝试在最后重新声明“newFeature”而不是最后一个“map”参数。然而,当重复的“历史”对象消失时,我不会得到“key”值,例如,对于第一个对象,key是“PLATFORM”。

vwkv1x7d

vwkv1x7d1#

这行代码是重复的原因:

feature&.map { |(k,v)| k=k , v=newFeature}

字符串
因为feature有2个元素。有一个简单的例子:

[0,1]&.map { |(k,v)| k=2 , v=3} =>>>> [[2, 3], [2, 3]]


在这种情况下,如果我们将feature放在数组中,并在末尾添加flatten将解决这个问题:

[feature]&.map { |(k,v)| k=k , v=newFeature}.flatten

相关问题