防止变换数组中的Nil值

时间:2018-09-16 04:13:55

标签: arrays json ruby ruby-on-rails-4 highcharts

删除nilsquite simple,但是,我想知道:

1)我在做什么错,为什么下面的数组结果包含nils

2)如何防止nils被添加到我的数组中,而不是事后将其删除。

@cars = Array.new
plucked_array = [
    [8, "Chevy", "Camaro", 20],
    [9, "Ford", "Mustang", 55],
    [9, "Ford", "Fusion", 150]
]
plucked_array.
    each { |id, make, model, model_count|
      @cars[id] ||= {name: make, id: make, data: []}
      @cars[id][:data].push([model, model_count])
    }
puts @cars.inspect
#=>[nil, nil, nil, nil, nil, nil, nil, nil, {:name=>"Chevy", :id=>"Chevy", :data=>[["Camaro", 20]]}, {:name=>"Ford", :id=>"Ford", :data=>[["Mustang", 55], ["Fusion", 150]]}]

puts @cars.compact.inspect
#=>[{:name=>"Chevy", :id=>"Chevy", :data=>[["Camaro", 20]]}, {:name=>"Ford", :id=>"Ford", :data=>[["Mustang", 55], ["Fusion", 150]]}]
# This gives the result I'm looking for, 
# just wondering how to best get transformed array without the post-cleanup.

我还尝试了@theTinMan的recommendationselect,然后是map,但结果却相同:

plucked_array.select { |id, make, model, model_count|
  @cars[id] = {'name' => make, 'id' => make, 'data' => []}
}.map { |id, make, model, model_count|
  @cars[id]['data'].push([model, model_count])
}
puts @cars.inspect
#=>[nil, nil, nil, nil, nil, nil, nil, nil, {:name=>"Chevy", :id=>"Chevy", :data=>[["Camaro", 20]]}, {:name=>"Ford", :id=>"Ford", :data=>[["Mustang", 55], ["Fusion", 150]]}]

我已经尝试部分成功地为@cars使用Hash而不是数组。这样可以避免出现nil,但是我的最终目标是在下面构建“ drilldown:series []”,它是一系列哈希值:

// Create the chart
Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Imaginary Car Stats'
    },
    subtitle: {
        text: 'Click the columns to view models.'
    },
    xAxis: {
        type: 'category'
    },
    yAxis: {
        title: {
            text: 'Total car count'
        }
    },
    legend: {
        enabled: false
    },
    plotOptions: {
        series: {
            borderWidth: 0,
            dataLabels: {
                enabled: true,
                format: '{point.y}'
            }
        }
    },
    tooltip: {
        headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
        pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
    },
    /*I have separate `pluck` query for this top-level series:*/
    "series": [
        {
            "name": "Cars",
            "colorByPoint": true,
            "data": [
                {
                    "name": "Ford",
                    "y": 205,
                    "drilldown": "Ford"
                },
                {
                    "name": "Chevy",
                    "y": 20,
                    "drilldown": "Chevy"
                },
                {
                    "name": "Other",
                    "y": 16,
                    "drilldown": null
                }
            ]
        }
    ],
    "drilldown": {
        /*This is the array of hashes I'm attempting to create:*/
        "series": [
            {
                "name": "Ford",
                "id": "Ford",
                "data": [
                    [
                        "Fusion",
                        150
                    ],
                    [
                        "Mustang",
                        55
                    ]
                ]
            },
            {
                "name": "Chevy",
                "id": "Chevy",
                "data": [
                    [
                        "Camaro",
                        20
                    ]
                ]
            }
        ]
    }
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
<script src="https://code.highcharts.com/modules/drilldown.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

3 个答案:

答案 0 :(得分:3)

您的代码的行为方式是因为array[8] = x在数组的位置8插入了x,而ruby用nil填充了最多8个空格。

a = []
a[7] = 4
a == [nil, nil, nil, nil, nil, nil, nil, 4]

您需要@cars作为哈希-而不是数组

我认为这会做您想要做的事情

plucked_array = [
    [8, "Chevy", "Camaro", 20],
    [9, "Ford", "Mustang", 55],
    [9, "Ford", "Fusion", 150]
]

cars = plucked_array.each_with_object({}) do |(id, make, model, count), cars|
  cars[id] ||= {id: id, make: make, data: []}
  cars[id][:data] << [model, count]
end

p cars.values

实际上与@Austio的解决方案几乎相同。

答案 1 :(得分:2)

我将更多地使用reduce方法,因为您正在获取列表并将其沸腾到另一个对象中。 each_with_object进行归约,但在每个循环中都隐式返回obj(在这种情况下为car)

new_list = plucked_array.each_with_object({}) do |(id, make, model, model_count), cars|
  # return before mutating cars hash if the car info is invalid
  cars[id] ||= {name: make, id: make, data: []}
  cars[id][:data].push([model, model_count])
end

# Then in your controller to handle the usage as an array
@cars = new_list.values

旁注:地图通常更多地用于变换或等效更改,这就是为什么我觉得这里不对。

答案 2 :(得分:1)

我建议按ID分组,然后创建一个hash

plucked_array.group_by(&:first).transform_values{ |v| v.map{ |id, make, model, model_count|  {name: make, id: make, data: [model, model_count]} }}

#  {8=>[{:name=>"Chevy", :id=>"Chevy", :data=>["Camaro ls", 20]}], 
#   9=>[{:name=>"Ford", :id=>"Ford", :data=>["Mustang", 55]}, {:name=>"Ford", :id=>"Ford", :data=>["Fusion", 150]}]}
#  }


编辑-更新与原始问题的编辑匹配(获取有关Highcharts的一系列数据)

这应该返回所需的结果:

plucked_array.map.with_object(Hash.new([])) { |(id, make, model, model_count), h|
  h[make] += [[model, model_count]] }.map { |k, v| {name: k, id: k, data: v }}

#=> [{:name=>"Chevy", :id=>"Chevy", :data=>[["Camaro", 20]]}, {:name=>"Ford", :id=>"Ford", :data=>[["Mustang", 55], ["Fusion", 150]]}]

第一部分构建像这样的哈希。

#=> {"Chevy"=>[["Camaro", 20]], "Ford"=>[["Mustang", 55], ["Fusion", 150]]}

Hash.new([])作为对象传递可以将元素插入默认数组。

最后,将哈希映射到所需的键。