美洲杯在哪买球:js对象合并,根据对象中相同的

简单、粗暴、直接上代码

<!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>

本文由美洲杯在哪买球发布于计算机教程,转载请注明出处:美洲杯在哪买球:js对象合并,根据对象中相同的

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。