user12919553
user12919553

Reputation:

Insert value on 0 index in dropdown

I have a app dropdown. private appDropdownOptions: IDropdownOption[]

I am assigning value to app dropdown from appItems.

this.appDropdownOptions = appItems.map(app => {
  return { key: app.appModuleIdUnique, text: app.name };
});

This is working fine , but i want to insert one default value on index 0.

{ key: this.appNoSelectionKey, text: 'Select' }

So how i can achieve it.

Upvotes: 1

Views: 304

Answers (3)

Balaj Khan
Balaj Khan

Reputation: 2570

You can add one default value at 0 index and rest of appItems using spread operator like below:

this.appDropdownOptions = [{ key: this.appNoSelectionKey, text: 'Select' }, ...appItems.map(app =>  { key: app.appModuleIdUnique, text: app.name})]

Upvotes: 1

SaimumIslam27
SaimumIslam27

Reputation: 1188

this.appDropdownOptions = appItems.map((app,index) => {
if(index===0){
return { key: this.appNoSelectionKey, text: 'Select' }
}
  return { key: app.appModuleIdUnique, text: app.name };
});

Upvotes: 0

Son Lai
Son Lai

Reputation: 21

Try unshift method

this.appDropdownOptions = appItems.map(app => {
  return { key: app.appModuleIdUnique, text: app.name };
});

this.appDropdownOptions.unshift({ key: this.appNoSelectionKey, text: 'Select' });

Upvotes: 0

Related Questions