JS Array.concat 连接多个值到数组

文章作者:中山市飞娥软件工作室「Feiesoft.com」  浏览次数:2990 次  更新日期:2013-10-30

<pre><script type="text/javascript">
//JS Array.concat 连接多个值到数组
//在JavaScript中,数组对象 Array的方法arr.concat(value,...)将所有参数添加到数组arr的一个副本中,并返回新数组。
//如果参数value是数组,则只添加value的元素。
var arr = [1,2,3];
var cat;

cat = arr.concat(2013,2014,2015,2016,["feiesoft.com","chenfeie.com","feiebook.com"],[[3001,3002],[8001,8002],9001]);

document.writeln(arr.join());//1,2,3
document.writeln(cat.join());//1,2,3,2013,2014,2015,2016,feiesoft.com,chenfeie.com,feiebook.com,3001,3002,8001,8002,9001

arr = arr.concat(arr);
document.writeln(arr.join());//1,2,3,1,2,3

var arr = [1,2,3];
arr.push(2013,2014,2015,2016,["feiesoft.com","chenfeie.com","feiebook.com"],[[3001,3002],[8001,8002],9001])
document.writeln(arr.join());//1,2,3,2013,2014,2015,2016,feiesoft.com,chenfeie.com,feiebook.com,3001,3002,8001,8002,9001


var arr = [2000,2008];
arr.push(arr);
for(var p in arr) document.writeln("arr." + p + "=" + arr[p] + " typeof: " + getType(arr[p])); //arr.0=2000 typeof: number
// arr.1=2008 typeof: number
// arr.2=2000,2008, typeof: Array

function getType(x){

 if(x == null)return "null";
 
 var t = typeof x;
 if(t != "object")return t;
 
 var c = Object.prototype.toString.apply(x);
 c = c.substring(8,c.length - 1);
 if(c != "Object")return c;
 
 if(x.constructor == Object)return c;
 
 if("classname" in x.constructor.prototype && typeof x.constructor.prototype.classname == "string")return x.constructor.prototype.classname;
 
 return "[unknown type]";

}
</script></pre>