Algorithm problem

From juejin. Cn/post / 684490…

Parse the URL Params as an object

let url = 'http://www.domain.com/?user=anonymous&id=123&id=456&city=%E5%8C%97%E4%BA%AC&enabled';
parseParam(url)
/* Result {user: 'anonymous', id: [123, 456], // Duplicate keys to array, can be converted to number type city: 'Beijing ', // Chinese decoding needs to be enabled: True, // The value key convention is not specified to be true} */
Copy the code

Implement a simple template rendering

let template = 'I am {{name}}, age {{age}}, gender {{sex}}';
let data = {
  name: 'name'.age: 18
}
render(template, data); // I am name, age 18, gender undefined
Copy the code
var proxy = new Proxy(target, handler);
` `= 'I ` let the template is {{name}}, {{age}} age, sex {{sex}}'; Let data = {name: 'name ', age: 18,}; console.log(render(template, data)); Function render(template, data) {let reg = /\{\{([a-z]+)\}\}/; function render(template, data) {let reg = /\{\{([a-z]+)\}\}/; if (reg.test(template)) { let item = reg.exec(template)[1]; template = template.replace(reg, data[item]); return render(template, data); } return template; #### slice(original array/string does not change, returns a truncated array) 1. String - if there is only one argument, cut from that argument to the end - if there are two arguments, from 2-7 (but not including 7) - if it is negative, -1 is the last character, and the front must be smaller than the end and does not contain the following number '` `javaScript
var str="Hello World";
var str1=str.slice(2); // If there is only one argument, extract all strings from the beginning to the end
var str2=str.slice(2.7); // With two arguments, extract a string with subscript 2 to a string with subscript 7 but no subscript 7
var str3=str.slice(-7, -2); // If it is negative, -1 is the last character of the string. Extracts a string starting at subscript -7 and not including subscript -2. The former must be less than the latter, otherwise an empty string is returned

console.log(str1); //llo World
console.log(str2); //llo W
console.log(str3); //o Wor
Copy the code
  1. An array of

With the string

  1. object

There is no

Splice (change array, return deleted array)

An array of

  • There are three parameters
  • The first argument, the starting position
  • The second parameter, number of deletions, will not be deleted if it is set to 0
  • The third parameter is to add the content of the new project

split

string

  • There are two arguments, the first argument is what character to split, the second argument is the maximum length of the returned array
  • If the first argument is an empty string, each character of the string is split
var str="AA BB CC DD";
var string1="Syntactic sugar for 1:2:3:4:5";
var str1=str.split("");// If an empty string ("") is used as a separator, each character of the string is split
var str2=str.split(""); // Use Spaces as delimiters
var str3=str.split("".4); //4 Specifies the maximum length of the returned array
var str4=string1.split(":");
console.log(str1); // ["A", "A", " ", "B", "B", " ", "C", "C", " ", "D", "D"]
console.log(str2); //["AA" "BB" "CC" "DD"]
console.log(str3); //["A", "A", " ", "B"]
console.log(str4); // ["1", "2", "3", "4", "5"]
Copy the code

join

Array concatenates arrays into strings and specifies a concatenate

var arr = [1.2.3];
console.log(arr.join());/ / 1, 2, 3
console.log(arr.join("-"));/ / 1-2-3
console.log(arr);// [1, 2, 3]
Copy the code