ES5/ES6将类数组 转化为 数组

把类数组转化为数组

  • 类数组转化为数组1 : Array.prototype.slice.call(arguments) || Array.prototype.slice.apply(arguments)
//从索引开始出提取数组
var test = function() {
	return Array.prototype.slice.call(arguments) ; //es5
}
let a = test(1,2,3,4)
console.log(a) // [1, 2, 3, 4]

//例二:
var test = function() {
	return Array.prototype.slice.apply(arguments) ; //es5
}
let a = test(1,2,3,4)
console.log(a) // [1, 2, 3, 4]

  • 类数组转化为数组2 : [].slice.call(arguments)
var test = function() {
	return [].slice.call(arguments) ; //es5
}
let a = test(1,2,3,4)
console.log(a) // [1, 2, 3, 4]

  • 类数组转化为数组3:Function.prototype.call.bind(Array.prototype.slice)
var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.call.bind(unboundSlice);

function test() {
  return slice(arguments);
}

var a = test(1, 2, 3,4); 
console.log(a)// [1, 2, 3, 4]
  • 类数组转化为数组4:[].concat.apply([],arguments)
var test = function(){
  return [].concat.apply([],arguments)
}

var a = test(1,2,3,4); 
console.log(a) //[1,2,3,4]
  • ES6类数组转化为数组 Array.from(arr)
var test = function(){
	return Array.from(arguments)
}
var a = test(1,2,3,4) ;
console.log(a) //[1,2,3,4]


function combine(){ 
    let arr = [].concat.apply([], arguments);  //没有去重复的新数组 
    return Array.from(new Set(arr));
} 

var m = [1, 2, 2], n = [2,3,3]; 
console.log(combine(m,n)); 

你可能感兴趣的:(js,数组)