如何将逗号分隔值打印到对象中?

时间:2019-03-12 15:17:26

标签: javascript

我有一些逗号分隔值(CSV)数据:

const thingsToDo =

name,location Notre Dame,Paris louver museum, Paris Botanical Garden,Montreal The Alamo, San Antonio Colosseum,Rome Eiffel Tower, Paris 我需要编写一个输出类似以下内容的函数:

  {
  Paris: [' Notre Dame ',' louver museum ', ' Eiffel Tower '],
  Montreal: [' Botanical Garden '],
  San Antonio: [' The Alamo '],
  Rome: [' Colosseum ']
}

这些值应按字母顺序排序。

Paris:[' Eiffel Tower ',' louver museum ',' Notre Dame ']

到目前为止,我已经做到了,但是我不知道如何打印出这样的确切输出:

const Paris = ['Notre Dame','louver museum','Eiffel Tower'];
const commaSeparated1 = Paris.filter(item => item).join(', ');
const Montreal = ['Botanical Garden'];
const commaSeparated2 = Montreal.filter(item => item).join(', ');
const SanAntonio = ['The Alamo'];
const commaSeparated3 = SanAntonio.filter(item => item).join(', ');
const Rome = ['Colosseum'];
const commaSeparated4 = Rome.filter(item => item).join(', ');

console.log(commaSeparated1);
console.log(commaSeparated2);
console.log(commaSeparated3);
console.log(commaSeparated4);

0 个答案:

没有答案