如何分配一系列字符串的键 /价值对
let arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue']
我想分配上面阵列中的字符串的键值
对:
[{drink: 'soda'},
{name: 'john'},
{someKey:'someValue'}
]
我花了几个小时来弄清楚这一点...我尝试使用地图方法来处理它,但是我对JavaScript的知识有限始终如一地返回错误...
如果有人可以帮助我,我将非常感谢。谢谢
let arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue']
I'd like to assign key value pairs of the strings i have in the array above
for example:
[{drink: 'soda'},
{name: 'john'},
{someKey:'someValue'}
]
I've spent hours trying to figure this out... I've tried approaching it with the map method but my limited knowledge on javascript is consistently returning errors...
I'd really appreciate it if someone could help me out. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以简单地进行循环:
You can do it with a simple for loop:
a “ nofollow noreferrer”>简单的旧school loop 逐步增加步骤每次迭代2。
注意:这将为您提供一个具有键/值对的对象,这使得(在我看来)比带有一个键/值对的对象更有用的数据结构。如果您正在寻找一系列对象 @sonetlumiere的答案是正确的。
A simple old-school loop increasing the step by 2 on each iteration.
Note: this will give you a single object with key/value pairs which makes (to my mind) a more useful data structure than an array of objects with one key/value pair each. If you're looking for an array of objects @sonEtLumiere's answer is the right one.
那将是最简单的方法:
或者,如果您真的想要一个淘汰者
That would be the easiest way:
Or if you really want a oneliner