立即注册 登录
召隆企博汇论坛 返回首页

至臻的个人空间 http://bbs.clogcn.com/?6 [收藏] [复制] [分享] [RSS]

日志

数组的字段合并

热度 1已有 458 次阅读2020-1-14 16:23 | html5

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

</body>
<script type="text/javascript">
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: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:1,rate:6,sessions:2,ratio:3},
    {lineid:5,rate:8,sessions:1,ratio:5},
    {lineid:7,rate:5,sessions:7,ratio:3}
];
var table3=[
    {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:1,rate:6,sessions:2,ratio:3},
    {lineid:5,rate:8,sessions:1,ratio:5},
    {lineid:7,rate:5,sessions:7,ratio:3}
];
  //根据lineid把table2合并到table1//
table2.forEach(function(o,d){
table1.forEach(function(t){
    if(t.lineid == o.lineid){
        t['rate_2'] = o['rate'];
    }
})
});
table3.forEach(function(o,d){
table1.forEach(function(t){
    if(t.lineid == o.lineid){
        t['rate_3'] = o['rate'];
    }
})
});
console.log(table1);
</script>
</html>


路过

雷人

握手
1

鲜花

鸡蛋

刚表态过的朋友 (1 人)

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

QQ|Archiver|手机版|小黑屋|召隆企博汇 ( 粤ICP备14061395号 )

GMT+8, 2024-11-23 23:35 , Processed in 0.019649 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部