如何分配一系列字符串的键 /价值对

发布于 2025-02-13 03:42:21 字数 308 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独自←快乐 2025-02-20 03:42:21

您可以简单地进行循环:

let arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue'];

let result = [];

for(let i = 0; i < arr.length; i+=2) {
  let obj = {}
  obj[arr[i]] = arr[i+1];
  result.push(obj)
}
console.log(result)

You can do it with a simple for loop:

let arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue'];

let result = [];

for(let i = 0; i < arr.length; i+=2) {
  let obj = {}
  obj[arr[i]] = arr[i+1];
  result.push(obj)
}
console.log(result)

尹雨沫 2025-02-20 03:42:21

a “ nofollow noreferrer”>简单的旧school loop 逐步增加步骤每次迭代2。

注意:这将为您提供一个具有键/值对的对象,这使得(在我看来)比带有一个键/值对的对象更有用的数据结构。如果您正在寻找一系列对象 @sonetlumiere的答案是正确的。

const arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue'];

// Initialise empty object
const out = {};

// Loop over the array in steps of 2 so
// that we can access `i` (the key) and 
// `i + 1` (the value) on each iteration
for (let i = 0; i < arr.length; i += 2) {
  out[arr[i]] = arr[i + 1];
}

console.log(out);
console.log(out.drink);
console.log(out.name);
console.log(out.someKey);

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.

const arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue'];

// Initialise empty object
const out = {};

// Loop over the array in steps of 2 so
// that we can access `i` (the key) and 
// `i + 1` (the value) on each iteration
for (let i = 0; i < arr.length; i += 2) {
  out[arr[i]] = arr[i + 1];
}

console.log(out);
console.log(out.drink);
console.log(out.name);
console.log(out.someKey);

小女人ら 2025-02-20 03:42:21

那将是最简单的方法:

const arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue']

const obj = {}
for (let i = 0; i < arr.length; i += 2) {
  obj[arr[i]] = arr[i + 1]
}

console.log(obj)

或者,如果您真的想要一个淘汰者

const arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue']
const result = arr.reduce((fin, str, i, arr) => i & 1 ? fin : { ...fin, [str]: arr[i + 1] }, {})
console.log(result)

That would be the easiest way:

const arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue']

const obj = {}
for (let i = 0; i < arr.length; i += 2) {
  obj[arr[i]] = arr[i + 1]
}

console.log(obj)

Or if you really want a oneliner

const arr = ['drink', 'soda', 'name', 'john', 'someKey', 'someValue']
const result = arr.reduce((fin, str, i, arr) => i & 1 ? fin : { ...fin, [str]: arr[i + 1] }, {})
console.log(result)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文