合併陣列

林堯彬發表於2020-04-04

方法一(Js):A.concat(B)

示例一:  

var a = ["a", "b", "c"];
var b = ["b", "c", "d"];
var c = a.concat(b);
console.log("合併:",c);
console.log("長:",c.length);
var d = $.unique(c); //jquery中的方法
console.log("去重:",d);
console.log("長:",d.length);

輸出:

 示例二:

var aObj = [
{ name : "小明",age : "21", sex : "男"},
{ name : "小紅",age : "20", sex : "女"},
{ name : "張三",age : "24", sex : "男"},
{ name : "思思",age : "22", sex : "女"},
];
var bObj = [
{ name : "韓雪",age : "21", sex : "女"},
{ name : "飄香",age : "20", sex : "男"},
{ name : "徐元",age : "24", sex : "男"},
{ name : "傾城",age : "23", sex : "女"},
];
var cObj = aObj.concat(bObj);
console.log("cObj",cObj);

輸出:

 

方法二(jQuery):$.merge(A,B)

示例一: 

var a = ["a", "b", "c"];
var b = ["b", "c", "d"];
var c = $.merge(a,b);
console.log("合併:",c);
console.log("長:",c.length);
var d = $.unique(c); //jquery中的方法
console.log("去重:",d);
console.log("長:",d.length);

輸出:

示例二:

 

var aObj = [
{ name : "小明",age : "21", sex : "男"},
{ name : "小紅",age : "20", sex : "女"},
{ name : "張三",age : "24", sex : "男"},
{ name : "思思",age : "22", sex : "女"},
];
var bObj = [
{ name : "韓雪",age : "21", sex : "女"},
{ name : "飄香",age : "20", sex : "男"},
{ name : "徐元",age : "24", sex : "男"},
{ name : "傾城",age : "23", sex : "女"},
];
var cObj = $.merge(aObj,bObj)
console.log("cObj",cObj);

 

輸出:

 

 

 

 

 



 

轉載於:https://www.cnblogs.com/lhleej/p/6906365.html

相關文章