Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const queryParams = {
- key: 'a',
- key2: 150,
- test: [1, 2, null, undefined, 3, 4],
- testStrings: ['first', 'second'],
- testNull: null,
- testBool: true,
- };
- let params = new HttpParams();
- const checkValueIsAppendable = (value): boolean => {
- return ['number', 'string', 'boolean'].includes(typeof value);
- };
- Object.entries(queryParams || {}).forEach(([key, value]) => {
- if (Array.isArray(value)) {
- value.filter(checkValueIsAppendable).forEach((arrayValue) => {
- params = params.append(`${key}[]`, arrayValue.toString());
- });
- } else if (checkValueIsAppendable(value)) {
- params = params.append(key, value.toString());
- }
- });
- console.log(params.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement