【发布时间】:2020-09-25 14:58:45
【问题描述】:
我的输入如下:
Input: keys = ['username', 'first-name', 'last-name', 'age', 'username'] and values = ['johndoe', 'John', 'Doe', 35, 'johnny']
我希望我的输出看起来像这样:
Output: {username: ['johndoe', 'johnny'], firstName: 'John', lastName: 'Doe', age: 35}
到目前为止,这是我的代码,我知道这并不能完全解决具有 'johndoe' 和 'johnny' 的用户名问题:
function mergeArrays(keys, values) {
var obj = {};
if (keys.length != values.length)
return null;
for (var index in keys)
obj[keys[index]] = values[index];
return obj;
}
【问题讨论】:
标签: javascript arrays dictionary object key-value