-1

I am trying to set a key value pair in the HttpParams that has a dynamic key value, for example:

let params = new HttpParams();
properties.forEach(p => {
    params = params.appendAll({ p.id : p.value})
}

But the p.id does not work as it expects to receive a string. how can I accomplish such result?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
herk
  • 15
  • 6

2 Answers2

1

params = params.appendAll({ [p.id] : p.value})

You need to bind it this way

Ziyed
  • 491
  • 2
  • 12
  • if i wanted to add a text before setting up the dynamic id? i.e. field-1 where the number 1 is the [p.id] ```params = params.appendAll({ 'field-1' : p.value})``` Is such thing possible? – herk Jul 25 '22 at 12:22
  • In this case I'd advise doing building the key first in a variable and then adding it there ```const key = `field-${p.id}`;``` then do ```{[key]: p.value}``` if you can't already map before hand the keys – Ziyed Jul 25 '22 at 12:56
0

If I understand properly what you want is to add the value of the variable as the key to do so you should do:

let params = new HttpParams();
properties.forEach(p => {
    params = params.appendAll({ [p.id] : p.value})
}
Toni Bardina Comas
  • 1,670
  • 1
  • 5
  • 15