JavaScript
x
32
32
1
ordersData = [
2
{ id: 100, name: 'order 1' },
3
{ id: 200, name: 'order 2' },
4
{ id: 300, name: 'order 3' },
5
{ id: 400, name: 'order 4' }
6
];
7
8
9
constructor( private objHelloService: HelloServiceService, private formBuilder: FormBuilder )
10
{
11
this.form = this.formBuilder.group({
12
orders: new FormArray([])
13
});
14
15
this.addCheckboxes();
16
}
17
18
private addCheckboxes()
19
{
20
this.ordersData.forEach((o, i) => {
21
const control = new FormControl(i === 0); // if first item set to true, else false
22
(this.form.controls.orders as FormArray).push(control);
23
});
24
}
25
26
27
submit()
28
{
29
const selectedOrderIds = this.form.value.orders.map((v:string, i:number) => v ? this.form.value.orders[i].id : null).filter(v => v !== null);
30
console.log(selectedOrderIds);
31
}
32
Problem is here:
JavaScript
1
2
1
.filter(v => v !== null)
2
Typescript is saying that I have not specified the type of v
.
What would be the type of v
here?
How to specify it?
Advertisement
Answer
depending on the orderData
schema it would be like this
JavaScript
1
2
1
{id: number, name: string}
2
but after you map it to an array of ID
so your schema would be like this
JavaScript
1
2
1
.filter((v: number) => v !== null)
2