I have the following code:
let urls = new Map<string, any[]>();
urls.set("1", ["param=1", "param2=2", "params3=3"]);
urls.set("2", ["param4=4", "param5=5"]);
function getUrl(): string {
let _tmp = [];
urls.forEach((e) => {
_tmp.push(e.join("&"));
});
return _tmp.join("&");
}
getUrl();
As result I need to get one plete string: param=1¶m2=2¶ms3=3¶m4=4¶m5=5
I dont like temprorary variable let _tmp = [];
, how to improve this?
I have the following code:
let urls = new Map<string, any[]>();
urls.set("1", ["param=1", "param2=2", "params3=3"]);
urls.set("2", ["param4=4", "param5=5"]);
function getUrl(): string {
let _tmp = [];
urls.forEach((e) => {
_tmp.push(e.join("&"));
});
return _tmp.join("&");
}
getUrl();
As result I need to get one plete string: param=1¶m2=2¶ms3=3¶m4=4¶m5=5
I dont like temprorary variable let _tmp = [];
, how to improve this?
- 1 You can map-reduce. – Mr. Polywhirl Commented Jul 14, 2020 at 11:54
- How to reduce Map()? – user13821408 Commented Jul 14, 2020 at 11:56
- use the array reduce method – Sunny Goel Commented Jul 14, 2020 at 11:57
-
2
What's the problem with the current approach? You can do
[...urls.values()].flat().join("&")
but is it really better? – VLAZ Commented Jul 14, 2020 at 11:58 - 2 Don't forget to escape all of the values otherwise the resulting URL might be invalid given certain characters. You'd be better of using URLSearchParams.append. – Jiří Pospíšil Commented Jul 14, 2020 at 12:06
4 Answers
Reset to default 3I think this should work for you ;)
let urls = new Map();
urls.set("1", ["param=1", "param2=2", "params3=3"]);
urls.set("2", ["param4=4", "param5=5"]);
let result = Array.from(urls.values()).map(arr => arr.join('&')).join('&');
console.log(result);
You can also achieve this a bit easier using .flat
, but then you have to add es2019
to lib section of tsconfig.json
, because .falt
is not in the official specification. More abut this you can read here: Typescript flatMap, flat, flatten doesn't exist on type any[]
Array.from(urls.values()).flat().join('&')
Is that what you need?
const urls = new Map()
urls.set("1", ["param=1", "param2=2", "params3=3"])
urls.set("2", ["param4=4", "param5=5"])
const result = [...urls.values()].flat().join('&')
console.log(result)
If you really need to use Map of arrays, then following will work:
let urls = new Map<string, any[]>();
urls.set("1", ["param=1", "param2=2", "params3=3"]);
urls.set("2", ["param4=4", "param5=5"]);
function getUrl(): string {
const sep = '&';
return Array.from(urls.values())
.map((params) => {
return params.join(sep)
})
.join(sep);
}
getUrl();
You can map the outer values and inside that, you can map the inner values while joining.
let urls = {
'1': [ 'param=1', 'param2=2', 'params3=3' ],
'2': [ 'param4=4', 'param5=5' ]
}
function getUrl() {
return Object.values(urls).map(values => values.join('&')).join('&')
}
console.log(getUrl())
Here is the TypeScript equivalent:
let urls = new Map<string, any[]>();
urls.set("1", ["param=1", "param2=2", "params3=3"]);
urls.set("2", ["param4=4", "param5=5"]);
function getUrl(): string {
return Array.from(urls.values()).map(values => values.join('&')).join('&')
}
console.log(getUrl())