最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do i assign keyvalue pairs of an array of strings - Stack Overflow

programmeradmin2浏览0评论
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

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

Share Improve this question asked Jul 5, 2022 at 16:38 moonmoon 431 gold badge2 silver badges8 bronze badges 3
  • It may be easiest to just use a "old-fashioned" for-loop and increment your iterator by 2 each time. – mykaf Commented Jul 5, 2022 at 16:39
  • Do you really want [{drink: 'soda'}, {name: 'john'}, {someKey:'someValue'}] as three separate objects in an array or do you want a single object with all those properties like { drink: 'soda', name: 'john', someKey: 'someValue' }? – Wyck Commented Jul 5, 2022 at 16:55
  • @Wyck as three seperate objects in an array.@sonEtLumiere gave the solution i needed. – moon Commented Jul 5, 2022 at 16:59
Add a ment  | 

3 Answers 3

Reset to default 3

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)

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);

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)

发布评论

评论列表(0)

  1. 暂无评论