• js面试题分享


    数组去重

    数字数组
    const arr1 = [1,2,3,4.4,32,121,4,5,5,6,2]
    function arrayUnique(array) {
    	return Array.from(new Set(arr1));
    }
    console.log(arrayUnique(arr1)); // [1, 2, 3, 4.4, 32, 121, 4, 5, 6]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    数组对象去重
    • 方案1

      const arr2 = [
      	{ name: 'a1', value: 1 },
      	{ name: 'a2', value: 1 },
      	{ name: 'a3', value: 1 },
      	{ name: 'a4', value: 1 },
      	{ name: 'a4', value: 1 },
      	{ name: 'a3', value: 1 },
      	{ name: 'a2', value: 1 },
      	{ name: 'a1', value: 1 },
      ];
      function arrayUnique(array, str) {
      	let obj = {};
      	return array.reduce((pre, cur) => {
      		obj[cur[str]] ? '' : obj[cur[str]] = pre.push(cur);
      		return pre;
      	}, [])
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
    • 方案2

      const arr2 = [
      	{ name: 'a1', value: 1 },
      	{ name: 'a2', value: 1 },
      	{ name: 'a3', value: 1 },
      	{ name: 'a4', value: 1 },
      	{ name: 'a4', value: 1 },
      	{ name: 'a3', value: 1 },
      	{ name: 'a2', value: 1 },
      	{ name: 'a1', value: 1 },
      ];
      function arrayUnique(array, str) {
      	let map = new Map();
      	array.forEach(ele => {
      		map.set(ele[str], ele);
      	});
      	return [...map.values()]
      }
      console.log(arrayUnique(arr2, 'name'));
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18

    给定一个url,转为location对象

    const url1 = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=mapbox';
    
    function createLocationObj(str) {
    	let obj = {};
    	const a1 = url1.split('://')
    	obj['protocol'] = a1[0];
    	let b;
    	if (a1[1].includes(':')) {
    		const a2 = a1[1].split(':');
    		obj['hostname'] = a2[0];
    		b = a2[1].split('/');
    		obj['port'] = b[0]
    	} else {
    		b = a1[1].split('/');
    		obj['hostname'] = b[0];
    		obj['port'] = obj.protocol === 'https' ? '443' : '8080';
    	}
    	const a3 = b[1].split('?');
    	obj['pathname'] = `/${a3[0]}`;
    	const a4 = a3[1].split('&');
    	let _params = {};
    	a4.forEach(ele => {
    		const a5 = ele.split('=');
    		_params[a5[0]] = a5[1]
    	});
    	obj['query'] = _params;
    	return obj;
    }
    console.log('-', createLocationObj(url1));
    // {
    // 	hostname: "www.baidu.com"
    // 	pathname: "/s"
    // 	port: "443"
    // 	protocol: "https"
    // 	query: {
    // 		f: "8"
    // 		ie: "utf-8"
    // 		rsv_bp: "1"
    // 		rsv_idx: "1"
    // 		tn: "baidu"
    // 		wd: "mapbox"
    // 	}
    // }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    第四代管网水位监测仪:高精度管网水位监测仪推荐
    SpringBoot集成阿里云OSS存储服务(普通上传、服务端签名上传)
    HashMap与ConcurrentMap
    Spring+spring mvc+mybatis整合的框架
    VR头显如何低延迟播放8K的RTSP|RTMP流
    电脑重装系统后如何设置 win11 的默认登录方式
    ARMday01(计算机理论、ARM理论)
    ElasticSearch - 初步检索
    Android开发之——Jetpack Compose布局(03)
    vue项目,如何关闭eslint检测?多种解决办法
  • 原文地址:https://blog.csdn.net/dfc_dfc/article/details/138162874