1

I have an array of data that I am using to create a drop down logic with.But there is a series of duplicate values when I create the drop-down using :

                    <select>
`                     `{
                        this.state.statistic_table.map((obj) => {
                            return <option value={obj.period}>{obj.period}</option>
                        })
                    }</select>

The long list of arrays contains a number of duplicate period values which are actually months. So in the dropdown I would get each and every value. I would like to eliminate the duplicates from the dropdown. How could I achieve that? Thanks

2

2 Answers 2

3

One way to acheive that could be to use the Set API after mapping your array :

{Array.from(new Set(this.state.statistic_table.map(obj => obj.period))).map(period => {
    return <option value={period}>{period}</option>
})}

And then converting it back to an array using Array.from to map your component with it

Sign up to request clarification or add additional context in comments.

1 Comment

A similar solution to the one below, but you directly gave me the solution I needed . Thanks. I'll keep this in mind.
1

I will fix that on this way:

1) Using some events e.g. componentDidMount where you need to filter your array on next :

define an empty array in the state:

this.state = {
 let filteredArray = [];
}

On mount filter them :

componentDidMount() {
    let uniq = Array.from(new Set(this.state.statistic_table));
    this.setState(uniq);
}

If the object type is correct you can do something like this:

 <select>{
      this.state.filteredArray.map((obj) => {
           return <option value={obj.period}>{obj.period}</option>
      })
 }</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.