Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我已将文本字符串拆分为数组:
var contact = text.split(',');
由于文本字符串始终包含相同顺序的单词,我想根据它们的顺序为每个单词分配一个键。最好的方法是什么?
下面的代码片段应该可以完成这项工作
var keys = ["firstKey", "secondKey", "thirdKey"], tmp = text.split(","), contact = {}; for (var i = 0; i < tmp.length && i < keys.length; i += 1) { contact[ keys[i] ] = tmp[i]; } console.log(contact);