我有以下哈希数组。我正在尝试遍历它,并构建一个ID和product_order_id值的哈希数组。
select *
from (
select *
,row_number() over(partition by date_trunc('day',a.readtime) order by a.readtime asc ) as rnk
from university.gas_ert a
)x
where x.rnk=1
这应该是它的样子
let db;
let request = window.indexedDB.open("Cities", 1);
request.onerror = function (event) {
console.log("error")
};
request.onsuccess = function (event) {
db = event.target.result;
document.getElementById('search').addEventListener('click',function () {
let transaction = db.transaction(["City"], "readwrite");
transaction.oncomplete = function () {
document.querySelector('body').innerHTML += '<li>Transaction completed.</li>';
};
transaction.onerror = function (event) {
document.querySelector('body').innerHTML += '<li>Transaction not opened due to error: ' + transaction.error + '</li>';
};
let objectStore = transaction.objectStore("City");
let objectStoreRequest = objectStore.getAll();
objectStoreRequest.onsuccess = function () {
document.querySelector('body').innerHTML += '<li>Request successful.</li>';
let myRecord;
myRecord = objectStoreRequest.result;
console.log(myRecord)
}
})
};
我尝试这样做,但是没有运气
objects =
[
#<Product: 0x00007ffd4a561108
@id="1",
@product_id="2",
@product_order_id="23",
@description="abc",
@status="abcde",
@start_date=nil,
@end_date=nil>,
#<Product: 0x00007ffd4a560c80
@id="45",
@product_id="22",
@product_order_id="87",
@description="ahef",
@status="gesff",
@start_date=nil,
@end_date=nil>
......more objects.....
]
有什么主意吗?
答案 0 :(得分:1)
内联解决方案:
> Hash[objects.map{|p| [p.id, p.product_order_id] }]
# Output : [{ 1=>23 }, { 45=>87 }]
答案 1 :(得分:1)
我通常会使用each_with_object
objects.each_with_object({}) { |obj, acc| acc[obj.id] = obj.product_order_id }
除非我真的想降低性能,否则我会选择Gagan's answer
答案 2 :(得分:0)
您尝试过吗?
def mapped_product(objects)
mapping = []
objects.each do |object|
mapping << {
object.id => object.product_order_id # I'm using an `=>` here
}
end
mapping # return the new mapping
end
我刚刚将:
的哈希值上的=>
更改为“使其动态”,并交换了id
和product_order_id
的值
您也可以在此处使用map
def mapped_product(objects)
objects.map do |object|
{ object.id => object.product_order_id }
end
end