Ruby Array of Arrays返回用于chartjs的查询,

时间:2018-02-14 02:03:18

标签: ruby-on-rails ruby

def self.return_this_data_for_map_method
    data = { :labels => [], datasets: [data: []] }
    dictionary = {}
    results.each do |teams|
        team = teams[0]
        teamMembers = teams[1]

        if dictionary[team].nil?
            dictionary[team] = teamMembers
        else 
            dictionary[team] += teamMembers

        end
    end
    data[:labels] << dictionary.keys
    data[:datasets][0][:data] << dictionary.values
    data
end

这是我要拿出的数据

 => {:labels=>[["CUBS", "CARDS", "ROCKIES", "ASTROS"]]:datasets=>[{:data=>[[72, 93, 74, 28]]}]}

这就是我试图获取数据的方式

=> {:labels=>["CUBS", "CARDS", "ROCKIES", "ASTROS"], :datasets=>[{:data=>[72, 93, 74, 28]}]}

它包装就像它仍然在一个阵列数组中,我还没有看到如何打破它的方式。关于如何修复我的代码的任何建议都将不胜感激。

使用ruby 2.3.1

2 个答案:

答案 0 :(得分:1)

最简单的解决方案是在返回数据之前展平数组:

+-------+-------+--------+-------------+----------+
|  id   | name  | phone  | is_important| client_id|
+-------+-------+--------+-------------+----------+
|   1   | test1 | 9999999|    1        |     1    |
+-------+-------+--------+-------------+----------+
|   2   | test2 | 8888888|    0        |     1    |
+-------+-------+--------+-------------+----------+

答案 1 :(得分:0)

@maxpleaner和@rails_id是正确的

function topLevel() {
  level2()
  .then(() => {
    console.log("topLevel resolved")
  })
}

let testError = true;

function level2() {
  if(testError) {
    // Added "return" here
    return new Promise((resolve, reject) => {
      resolve("Level 2");
    })
    .then(() => {
      return (level3());
    })
  }
  else {
    return (level3());
  }
}

function level3() {
  return (new Promise((resolve, reject) => {
    resolve("Level 3");
  }));
}