List Format

Author
Ex-Meta Staff Engineer
Languages

Given a list of strings, implement a function listFormat that returns the items concatenated into a single string. A common use case would be in summarizing the reactions for social media posts.

The function should support a few options as the second parameter:

  • sorted: Sorts the items by alphabetical order.
  • length: Show only the first length items, using "and X other(s)" for the remaining. Ignore invalid values (negative, 0, etc).
  • unique: Remove duplicate items.

Examples

listFormat([]); // ''
listFormat(['Bob']); // 'Bob'
listFormat(['Bob', 'Alice']); // 'Bob and Alice'
listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John']);
// 'Bob, Ben, Tim, Jane and John'
listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John'], {
length: 3,
}); // 'Bob, Ben, Tim and 2 others'
listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John'], {
length: 4,
}); // 'Bob, Ben, Tim, Jane and 1 other'
listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John'], {
length: 3,
sorted: true,
}); // 'Ben, Bob, Jane and 2 others'
listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John', 'Bob'], {
length: 3,
unique: true,
}); // 'Bob, Ben, Tim and 2 others'
listFormat(['Bob', 'Ben', 'Tim', 'Jane', 'John'], {
length: 3,
unique: true,
}); // 'Bob, Ben, Tim and 2 others'
listFormat(['Bob', 'Ben', '', '', 'John']); // 'Bob, Ben and John'

Companies

Premium FeaturePurchase premium to see companies which ask this question.
View plans