簡單、粗暴、直接上代碼 ...
簡單、粗暴、直接上代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta name="format-detection" content="telephone=no" />
<title>對象合併</title>
</head>
<body>
<script>
var table1=[
{lineid:1,rate:6,sessions:2,ratio:3},
{lineid:5,rate:8,sessions:1,ratio:5},
{lineid:9,rate:4,sessions:2,ratio:2},
{lineid:2,rate:1,sessions:3,ratio:3},
{lineid:"99",rate:2,sessions:5,ratio:1},
{lineid:"a",rate:4,sessions:3,ratio:9},
{lineid:7,rate:5,sessions:7,ratio:3},
];
var table2=[
{lineid:9,rate_2:4,sessions_2:2,ratio_2:2},
{lineid:2,rate_2:1,sessions_2:3,ratio_2:3},
{lineid:"99",rate_2:2,sessions_2:5,ratio_2:1},
{lineid:"a",rate_2:4,sessions_2:3,ratio_2:9},
{lineid:1,rate_2:6,sessions_2:2,ratio_2:3},
{lineid:5,rate_2:8,sessions_2:1,ratio_2:5},
{lineid:7,rate_2:5,sessions_2:7,ratio_2:3}
];
//根據lineid把table2合併到table1//
table2.forEach(function(o,d){
for(var k in o){
table1.forEach(function(t){
for(var key in t){
if(t.lineid == o.lineid){
t[k] = o[k];
}
}
})
}
});
console.log(table1);
</script>
</body>
</html>