1

I have an angular component where I using input and list to show an array.

Here is html of component

 <div class="form-group col-md-6">
                            <label>{{ l('Tools') }}</label>
                            <div class="row mt-2">
                                <div class="col-8">
                                    <input
                                        #toolsInput="ngModel"
                                        [(ngModel)]="tool"
                                        class="form-control"
                                        type="text"
                                        name="tools"
                                        maxlength="32"
                                    />
                                </div>
                                <div class="col-4">
                                    <button
                                        [disabled]="tool == null || tool == '' || tool.trim() == ''"
                                        type="button"
                                        class="btn btn-primary"
                                        (click)="addItemToTools()"
                                    >
                                        <i class="fa fa-plus"></i>
                                    </button>
                                </div>
                            </div>
                            <div class="row mt-2">
                                <div class="col-12">
                                    <p-listbox
                                        [readonly]="true"
                                        [options]="tools"
                                        [style]="{
                                            width: '85%',
                                            height: '200px',
                                            overflow: 'auto',
                                            'overflow-y': 'scroll'
                                        }"
                                    >
                                        <ng-template let-tool let-i="index" pTemplate="item">
                                            <div class="row" style="margin-right: 0px; margin-left: 0px;">
                                                <div class="col-9">{{ tool.value }}</div>
                                                <div class="col-1">
                                                    <button
                                                        type="button"
                                                        class="btn btn-primary"
                                                        (click)="removeTool(i)"
                                                    >
                                                        <i class="fa fa-times"></i>
                                                    </button>
                                                </div>
                                            </div>
                                        </ng-template>
                                    </p-listbox>
                                </div>
                            </div>
                        </div>

Here is TS code of my component (adding tools part)

export class CreateJobComponent extends CreateEditModalComponentBase implements OnInit {
tools: SelectItem[] = [];
 addItemToTools(): void {
    this.tools.push({ value: this.tool, label: this.tool });
    this.tool = null;
}

And in save method I map this to DTO array like this -

this.job.tools = this.tools.map(e => e.value);

But if array is empty, it maps empty value. How I can fix this?

2
  • "if array is empty, it maps empty value." What is your requirement? Commented Nov 27, 2019 at 10:37
  • I need to map array if array has values only @AdritaSharma Commented Nov 27, 2019 at 10:38

2 Answers 2

1

Try like this:

if(this.tools.length > 0)
  this.job.tools = this.tools.map(e => e.value)
Sign up to request clarification or add additional context in comments.

Comments

0

just try with :

       if(array.length>0){ array.map....
       }else{...}

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.