最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Join Map values into single string - Stack Overflow

programmeradmin0浏览0评论

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&param2=2&params3=3&param4=4&param5=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&param2=2&params3=3&param4=4&param5=5

I dont like temprorary variable let _tmp = []; , how to improve this?

Share Improve this question edited Jul 14, 2020 at 12:17 Yevhen Horbunkov 15.6k3 gold badges27 silver badges45 bronze badges asked Jul 14, 2020 at 11:53 user13821408user13821408 6
  • 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
 |  Show 1 more ment

4 Answers 4

Reset to default 3

I 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())
发布评论

评论列表(0)

  1. 暂无评论