gpt4 book ai didi

arrays - 将数组元素映射到哈希值 Ruby

转载 作者:太空宇宙 更新时间:2023-11-03 17:21:29 25 4
gpt4 key购买 nike

我这里有一个哈希,

    @property_hash = {
:code => '',
:fname => '',
:lname => '',
:basic_sal => '',
:emp_type => '',
}

和一个数组

line = [02,'Firstname', 'LastName', 5800, 'PL']

我想像这样把数组映射到散列中

@property_hash = {
:code => 02,
:fname => 'Firstname',
:lname => 'LastName',
:basic_sal => 5800,
:emp_type => 'PL',
}

最好的方法是什么?? 谢谢

最佳答案

你可以这样试试:

 @property_hash.each_with_index {|(k, v), index| @property_hash[k] = line[index]}

不是最好的方法,但那会起作用

关于arrays - 将数组元素映射到哈希值 Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36720161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com