This article is participating in the nuggets team number online activity, click to see the dachang spring recruiting positions

I. Title Description:

Implement a URL parsing parameter method, obtain the parameter as a dictionary object

  1. Words are separated by Spaces. If there are multiple Spaces, only one space is reserved.
  2. Remove all Spaces at the beginning and end of the string;
function parse (url) {
    // Your code implementation
}
parse("https://www.xx.com?a=1&b=2&p=bottom") returns {query: {a:1.b:2
    },
    hash: {p:"bottom"}}Copy the code

Ii. Analysis of Ideas:

General topic parsing URL; Investigate the js foundation;

Iii. AC Code:

  let url = 'https://www.asss.com?a=1&b=2#c=3';
  function forArray(arr) {
    let obj = {};
    const newArr = arr.map((v) = > {
      let a = v.split('=');
      obj[a[0]] = a[1];
      return obj;
    });
    return obj;
  }
  function parse(url) {
    if (url && typeof url === 'string') {
      let query = url.substring(url.indexOf('? ') + 1, url.indexOf(The '#')).split('&');
      let hash = url.substring(url.indexOf(The '#') + 1, url.length).split('&');
      return {
        query: forArray(query),
        hash: forArray(hash), }; }}console.log(parse(url));
Copy the code

Iv. Summary:

Try to get to a Nuggets event and check in;