Home > OS >  Remove nil values from array and corresponding entries from reference array
Remove nil values from array and corresponding entries from reference array

Time:05-02

I have two arrays generated from :

@dividends_values = @dividends.historical.map(&:dividend).reverse.last(50)
@dividends_dates = @dividends.historical.map(&:date).reverse.last(50)

The first array is an array of float values and occasional there can be a few nil entries in that. I want to remove those nil entries(which is pretty easy with a compact or something like that), but I also want to move the corresponding entries from the @dividends_dates array.

That is because they the dates array is a 1-1 reference to the values array, so index 0 of array with dates correspondings to index 0 of array with values.

What is a good way to do that?

CodePudding user response:

First, filter by nil. Then break that up into two arrays.

@last_dividends = @dividends.historical.select { |d| d.dividend }
@dividends_values = @last_dividends.map(&:dividend)
@dividends_dates = @last_dividends.map(&:date)

Better yet, turn them into a single array of [[dividend, date], [...]]

@last_dividends = @dividends
  .historical
  .select { |d| d.dividend }
  .map { |d| [d.dividend, d.date] }

CodePudding user response:

First let's create a class-like object for illustration.

Dividend = Struct.new(:value, :date) {}
historical = [
  Dividend.new(nil, "Jan 1"),
  Dividend.new(10,  "Mar 22"),
  Dividend.new(13,  "Apr 21"),
  Dividend.new(nil, "Aug 7"),
  Dividend.new(8,   "Oct 11")
]
  #=> [#<struct Dividend value=nil, dade="Jan 1">,
  #    #<struct Dividend value=10,  date="Mar 22">,
  #    #<struct Dividend value=13,  date="Apr 21">,
  #    #<struct Dividend value=nil, date="Aug 7">,
  #    #<struct Dividend value=8,   date="Oct 11">]

Then, for example,

  inst = historical[3]
    #=> #<struct Dividend value=nil, date="Aug 7">
  inst.valued
    #=> nil
  inst.date
    #=> "Aug 7"

We may write

historical.filter_map do |inst|
  [inst.value, inst.date] unless inst.value.nil?
end.transpose
  #=> [[10, 13, 8], ["Mar 22", "Apr 21", "Oct 11"]]

Note that

historical.filter_map do |inst|
  [inst.value, inst.date] unless inst.value.nil?
end
  #=> [[10, "Mar 22"], [13, "Apr 21"], [8, "Oct 11"]]

See Enumerable#filter_map.

  •  Tags:  
  • ruby
  • Related