Hash1 = {
map1: {city: 'Berlin', country: 'Germany'},
map2: {city: 'Toronto', country: 'Canada'},
map3: {city: 'Boston', country: 'USA' }
}
好的,所以我想构建一个如下所示的哈希:
newHash = {
Berlin: 'map1',
Toronto: 'map2',
Boston: 'map3'
}
如何循环浏览Hash1
并构建newHash
。
Hash1.each_with_object({}) do |(key, value), acc|
# what do I do inside this loop?
end
答案 0 :(得分:2)
gulp.task("uglify", ["copy-lib"],
function () {
return gulp.src([paths.wwwroot.lib + "/jquery.js", paths.wwwroot.lib + "/bootstrap.js"])
.pipe(sourcemaps.init())
.pipe(concat("bundle.js"))
.pipe(uglify())
.pipe(isDebug, sourcemaps.write())
.pipe(rename(addMinExtension))
.pipe(gulp.dest(paths.wwwroot.lib));
});
答案 1 :(得分:1)
#include <iostream>
struct A {
int a;
int b;
};
static A a[2]{{1, 2}, {3, 4}};
class foo {
public:
int c;
public:
template<typename F>
foo(int index, F getter) { c = getter(a[index]); }
};
int main() {
auto agetter = [](const A& a){ return a.a; };
auto bgetter = [](const A& a){ return a.b; };
foo const f1(0, agetter);
std::cout << f1.c << "\n";
foo const f2(0, bgetter);
std::cout << f2.c << "\n";
foo const f3(1, agetter);
std::cout << f3.c << "\n";
foo const f4(1, bgetter);
std::cout << f4.c << "\n";
}
答案 2 :(得分:0)
我只展示了以下众多方法中的一种
new_hash = Hash.new
Hash1.each do |key, value|
new_hash[value[:city]] = key
end
答案 3 :(得分:0)
output = {}
hash.each do |key, value|
output[:"#{value[:city]}"] = key.to_s
end
puts output
# {:Berlin=>"map1", :Toronto=>"map2", :Boston=>"map3"}
对于确切的输出样式,您必须使用Ruby 1.9或更高版本。