Search code examples
reactjstypescriptfluent-uifluentui-react

React Combobox SPFx map out only unique values instead


As the title states I am trying to map out only unique values instead of the same repeating values. My code where I map my data through can be seen below:

<ComboBox label='City' options={rooms.map((roomInfo): IComboBoxOption => {
return { key: roomInfo.id, text: roomInfo.location }})}/>

The data the combobox shows with following code above is something like:

location: New York
location: New York
location: Chicago
location: California
location: Chicago

What I want is:

location: New York
location: California
location: Chicago

Solution

  • If I understand well you have duplicate values inside rooms array, you can remove duplicates:

    const data = [
      { id: 1, location: 'New York' },
      { id: 2, location: 'New York' },
      { id: 3, location: 'Chicago' },
      { id: 4, location: 'California' },
    ]
    
    const noDuplicates = data.filter((roomInfo, i, arr) => arr.findIndex(x => x.location === roomInfo.location) === i)