简体   繁体   中英

How to convert array to object for queryparams?

I have this array['ca','ba','es']

How can i covnert to object so that i have r=ca, r=ba, r=es.

So in queryParams when i add i will have url something like this:

r=ca&r=ba&r=es

I tried something like this but its not working:

let obj = this.queries.r.reduce(function(acc, cur, i) {
      'r' = cur; 
      return acc;
    }, {});

Because r is not valid as param.Any suggestion how can i fix this?

Try this. I think this should work in your case.

Const params = new HttpParams({fromObject: { r: yourArrayHere }}).

Make sure you import HttpParams from @angular.

Hope that helps!

Since you are using an old version. Latest HttpParams fromObject won't work.

Try something like this.

let params = new HttpParams()

['ca','ba','es'].forEach(item => {
     params = params.append(“r”, item);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM