List deduplication
Using Set Data Structure
const set = new Set([2, 8, 3, 8, 5])
Note: The Set data structure believes that objects are never equal, and even two empty objects are not equal inside the Set structure.
Method encapsulation
const uniqueness = (data, key) => { const hash = new Map() return (item => !(item[key]) && (item[key], 1)) }
Convert object to query string
Code Comments
/** * @description: Convert object to query string * @params {Object} data: Source data * @return {String} Target data * @example * * serialize({ a: 1, b: 2 }) * * a=1&b=2 */
Method encapsulation
export const serialize = data => { return (data).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`).join('&') }
Get query parameters
Code Comments
/** * @description: Get query parameters * @params {String} name: query parameter name * @return {String} Target data * @example * * getQueryString('id') */
Method encapsulation
export const getQueryString = key => { return (new URLSearchParams()).get(key) }
Extended Package
class Route { static url = new URL(location) static attr(attribute) { return [attribute] } static getParams() { const { searchParams } = , params = {} for (const [key, value] of ()) { params[key] = value } return params } static getParam(name) { const { searchParams } = return (name) } static hasParam(name) { const { searchParams } = return (name) } }
The above is the detailed content of JS data analysis data deduplication and parameter serialization examples. For more information about JS data deduplication parameter serialization, please pay attention to my other related articles!