🌈 自定义 unshift

var arr = [1, 2, 3];

Array.prototype.myUnshift = function() {
  // 在数组打头,添加一个或多个元素,最后返回最新数组的长度
  var len = arguments.length;
  for (let i = len - 1; i >= 0; i--) {
    const element = arguments[i];
    this.splice(0, 0, element);
  }
  return this.length;
}

// console.log(arr.unshift(3, 2, 1), arr);
console.log(arr.myUnshift(3), arr);
var arr = [1, 2, 3];

Array.prototype.myUnshift = function() {
  var pos = 0;
  for (let i = 0; i < arguments.length; i++) {
    const element = arguments[i];
    this.splice(pos, 0, element);
    pos++;
  }
  
  return this.length;
}

// console.log(arr.unshift(3, 2, 1), arr);
console.log(arr.myUnshift(3, 2, 1), arr);
var arr = ['d', 'e', 'f'];
Array.prototype.myUnshift = function(){
	var argArr = Array.prototype.slice.call(arguments); 
  return argArr.concat(this);
}
var newArr = arr.myUnshift('a', 'b', 'c');
console.log(newArr);

🌈 数组按照元素的总字节数长度排序

var arr = ['Are you OK', 'Lance', 'Jerry', '我爱你'];
// unicode 0-255 1个字节;256 2个字节

var getBytes = function(str) {
	// 先把每个字符都当一个字节算
  var bytes = str.length;
  
  for (var i = 0; i < str.length; i++) {
  	if (str.charCodeAt(i) > 255) {
    	bytes++;
    }
  }
  
  return bytes;
}

arr.sort(function(a, b) {
	return getBytes(a) - getBytes(b);
});
console.log(arr);

🌈 封装自己的 typeof

Object.prototype.toString.call()

function myTypeof(val) {
	var type = typeof(val);
  var toStr = Object.prototype.toString;
  var map = {
  	'[object Object]': 'object-object',
    '[object Array]': 'object-array',
    '[object String]': 'object-string',
    '[object Number]': 'object-number',
    '[object Boolean]': 'object-boolean'
  }
  
  if (val === null) {
  	return 'null';
  } else if (type === 'object') {
  	var ret = toStr.call(val);
    return map[ret];
  } else {
  	return type;
  }
}

console.log(myTypeof(function () {}));
console.log(myTypeof(1));
console.log(myTypeof([]));
console.log(myTypeof(true));
console.log(myTypeof(null));
console.log(myTypeof(new String('233')));

🌈 数组去重

1、循环 通过 hasOwnProperty 判断新数组是否有该属性(问题:{}会被解析成'[object Object]')
var arr = [
  {}, {}, 
  '', '',
  233, 233, '233',
  'abc',
  undefined, 
  null, null, 
  NaN, NaN, 
  123, 
  [2], [2], 
  [2, 3]];

Array.prototype.myUnique = function() {
	var hash = {},
      newArr = [];
  for (var i = 0; i < this.length; i++) {
    var item = this[i];
  	if (!hash.hasOwnProperty(typeof item + JSON.stringify(item))) {
    	hash[typeof item + JSON.stringify(item)] = item;
      newArr.push(item);
    }
  }
  
  return newArr;
};

console.log(arr.myUnique());
// [
//   {},       '',
//   233,      '233',
//   'abc',    undefined,
//   null,     NaN,
//   123,      [ 2 ],
//   [ 2, 3 ]
// ]
2.ES6 Set
Array.prototype.myUnique = function() {
	return Array.from(new Set(this));
};

// [
//   {},        {},
//   '',        233,
//   '233',     'abc',
//   undefined, null,
//   NaN,       123,
//   [ 2 ],     [ 2 ],
//   [ 2, 3 ]
// ]
3.利用 includes 检测数组是否有某个值  或者 indexOf 判断索引
Array.prototype.myUnique = function() {
	let arr = [];
  for (let i = 0; i < this.length; i++) {
    if (!arr.includes(this[i])) {
      arr.push(this[i]);
    }
  }
  return arr;
};

// [
//   {},        {},
//   '',        233,
//   '233',     'abc',
//   undefined, null,
//   NaN,       123,
//   [ 2 ],     [ 2 ],
//   [ 2, 3 ]
// ]
4. Map
Array.prototype.myUnique = function() {
  let map = new Map(), arr = [];
  for (let i = 0; i < this.length; i++) {
    if (!map.has(this[i])) {
      map.set(this[i], this[i]);
      arr.push(this[i]);
    }
  }
  return arr;
}

// [
//   {},        {},
//   '',        233,
//   '233',     'abc',
//   undefined, null,
//   NaN,       123,
//   [ 2 ],     [ 2 ],
//   [ 2, 3 ]
// ]
5.filter (问题:无法识别 NaN)
Array.prototype.myUnique = function() {
  return this.filter((v, idx) => {
    return this.indexOf(v, 0) === idx;
  });
}

// [
//   {},        {},
//   '',        233,
//   '233',     'abc',
//   undefined, null,
//   123,       [ 2 ],
//   [ 2 ],     [ 2, 3 ]
// ]

字符串去重

var str = "111222333000aaa我我我";

String.prototype.myUnique = function() {
	var hash = {},
      newStr = "";
  
  for (var i = 0; i < this.length; i++) {
  	if (!hash.hasOwnProperty(this[i])) {
    	hash[this[i]] = this[i];
      newStr += this[i];
    }
  }
  
  return newStr;
};

console.log(str.myUnique());

找出一串字符串中所有只出现一次的字符的索引组成的数组

var str = "saso1idangvcas3gkxkxlaknzgegunclas";

function test(str) {
	var hash = {},
      newArr = [];
  
  for (var i = 0; i < str.length; i++) {
    var item = str[i];
  	if (!hash.hasOwnProperty(item)) {
    	hash[item] = 1;
    } else {
    	hash[item]++;
    }
  }
  
  console.log(hash);
  
  for (var i = 0; i < str.length; i++) {
  	if (hash[str[i]] === 1) {
      console.log(str[i]);
    	newArr.push(i);
    }
  }
  
  return newArr;
}

console.log(test(str));