手掌心
根据ECMAScript 6ES6,干净的方法是破坏数组:const input = 'john smith~123 Street~Apt 4~New York~NY~12345';const [name, street, unit, city, state, zip] = input.split('~');console.log(name); // john smithconsole.log(street); // 123 Streetconsole.log(unit); // Apt 4console.log(city); // New Yorkconsole.log(state); // NYconsole.log(zip); // 12345输入字符串中可能有额外的项。在这种情况下,您可以使用REST操作符为REST获取一个数组,或者直接忽略它们:const input = 'john smith~123 Street~Apt 4~New York~NY~12345';const [name, street, ...others] = input.split('~');console.log(name); // john smithconsole.log(street); // 123 Streetconsole.log(others); // ["Apt 4", "New York", "NY", "12345"]我假设值为只读引用,并使用const申报。享受ES6!