0

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?

4
  • 1
    ['ca','ba','es'].map(s => r=${s}).join("&") Commented Jul 17, 2018 at 22:06
  • this is not working...im getting error that can not find name r, $ .... Commented Jul 17, 2018 at 22:09
  • ['ca','ba','es'].map(s => `r=${s}`).join("&") Commented Jul 17, 2018 at 22:14
  • and how can i assign this to queryParam with rest of params? because right now i get [0]:r [1]:= [2]:c [3]:a . .... and so on Commented Jul 17, 2018 at 22:19

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

7 Comments

im getting this: argument of type '{ fromObject: { r: any; }; }' is not assignable to parameter of type '{ fromString?: string; encoder?: HttpParameterCodec; }'. Object literal may only specify known properties, and 'fromObject' does not exist in type '{ fromString?: string; encoder?: HttpParameterCodec; }'. (property) OfferComponent.queries: any
Can you provide a line of example including your array?
let a = []; const params = new HttpParams({fromObject: { r: a}})
What version of Angular are you using? This functionality was added recently and might not be available if you are running old version. As per Angular code above example should work. See here: github.com/angular/angular/blob/6.0.9/packages/common/http/src/…
"@angular/cli": "1.2.3", "@angular/compiler-cli": "^4.2.6",
|
0

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);
}

6 Comments

and then what ? because then im getting array of objects and not object..
You can forEach over an array of objects same way I have shown. You get the object inside forEach callback function. You can access the value using whatever object key you want. This params variable then can be used with http.get and http.post.
HttpParams {updates: Array(2), cloneFrom: HttpParams, encoder: HttpUrlEncodingCodec, map: null} cloneFrom : HttpParams {updates: null, cloneFrom: null, encoder: HttpUrlEncodingCodec, map: null} encoder : HttpUrlEncodingCodec {} map : null updates : Array(2) 0 : {param: "r", value: "ca", op: "a"} 1 : {param: "r", value: "ba", op: "a"} length : 2
thats all i get when i console.log(params)
i want to do something like this Object.assign(obj, params) so in obj i will have r=ca & r=ba and so on
|

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.