我正在使用以下代码try 重新映射位于多个对象数组内的"usageByMonth"array.

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": []
    }
    
  }
}

任何帮助都是非常感谢的!

我try 在结尾处重新声明"newFeature",而不是最后一个"map"参数.然而,当重复的"历史"对象消失时,发生这种情况时,我无法获得"key"值,例如,对于第一个对象,键是"Platform".

推荐答案

下面这行代码是造成混淆的原因:

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

因为Feature有两个元素.下面是一个简单的例子:

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

在这种情况下,如果我们将Feature放在一个数组中,并在末尾添加Flatten可以解决这个问题:

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

Ruby相关问答推荐

Ruby插值法导致无序输出

Rails 7 - 访问连接模型生成新查询

定义Struct时如何指定成员的类型?

当一个方法在没有参数的 return 关键字上提前退出时,返回什么值?

Windows powershell文件类型编码中Ruby IO流的原因是什么

为什么我的二维 Ruby 数组的多个值会发生变化,尽管只更改了其中一个?

有没有办法在 Capybara 中保持登录状态?

ruby `encode': "\xC3" 从 ASCII-8BIT 到 UTF-8 (Encoding::UndefinedConversionError)

在 Ruby 中为类添加实例变量

方法如何在 Ruby 中使用哈希参数?

Ruby:从字节创建一个字符串

RSpec: stubbing Kernel::sleep?

你喜欢哪种风格的 Ruby 字符串引用?

仅针对特定参数的 Rspec 存根方法

删除/取消定义类方法

如何强制 Ruby 字符串为 n 个字符

Ruby 数组中的奇数(或偶数)条目

如何合并 Ruby 哈希

Ruby NOT Rails 中有复数函数吗?

如何从字符串创建 Ruby 日期对象?