let person = {}
let keys = ['name', 'age', 'work'];
let values = ['Adam', 23, 'none'];
// how to make result
person = {
'name': 'Adam',
'age': 23
'work': 'none'
}
Asked
Active
Viewed 30 times
-4

jonrsharpe
- 115,751
- 26
- 228
- 437
-
What have you tried, and what exactly is the problem with it? Any research, even - have you read e.g. https://stackoverflow.com/q/39127989/3001761? – jonrsharpe Apr 12 '22 at 21:34
-
i have array of keys and array of values and i want create an object with them – Adam Benson Apr 12 '22 at 21:35
-
Yes, I understand the desired outcome, but this isn't a code-writing or tutorial service. Please take the [tour] and read [ask]. – jonrsharpe Apr 12 '22 at 21:36
1 Answers
-2
A simple loop does just fine.
let person = {}
let keys = ['name', 'age', 'work']; let values = ['Adam', 23, 'none'];
for (const i of Array(keys.length).keys()) {
const key = keys[i];
const val = values[i];
person[key] = val;
}
console.log(person);

async await
- 1,967
- 1
- 8
- 19
-
1
-
-
-
@jonrsharpe why? Trying out some stuff I read on [this post](https://stackoverflow.com/questions/8273047/javascript-function-similar-to-python-range) – async await Apr 12 '22 at 21:41
-
It's because the code obscures the nature of the loop. `for (let i = 0; i < keys.length; i++) {...}` is more readable. Also that post is from 2011. – Andy Apr 12 '22 at 21:58
-
1