javascript每个覆盖数组对象

时间:2015-11-14 05:36:23

标签: javascript arrays

这是我的代码

$(function() {
  var sqlTable = [
    {
      name: 'a',
      TagName: 'a_1'
    },
    {
      name: 'b',
      TagName: 'b_1'
    }
  ];

  var hbaseTable = [
    {
      TagName: 'a_12015',
      Tvalue: '1'
    },
    {
      TagName: 'a_12016',
      Tvalue: '2'
    },
    {
      TagName: 'b_12015',
      Tvalue: '1'
    },
    {
      TagName: 'b_12016',
      Tvalue: '3'
    }
  ];

  var new_result = temp(sqlTable, hbaseTable);

  function temp(a, b) {
    var new_result2 = [];
    var k = 0;

    for (var i=0; i<a.length; i++) {
      var sql_value = a[i];
      var tag_name = sql_value.TagName;

      for (var j=0; j<b.length; j++) {
        var hbase_value = b[j];
        var hbase_tag = hbase_value.TagName;
        var hbase_tvalue = '';
        var hbase_tag_name = '';

        console.log('/', hbase_tag + ', ' + tag_name);
        if (hbase_tag.indexOf(tag_name) > -1)
        {
          var t_sql_value = sql_value;
          hbase_tvalue = hbase_value.Tvalue;
          hbase_tag_name = hbase_value.TagName;
          t_sql_value.hbase_tvalue = hbase_tvalue;
          t_sql_value.hbase_tag_name = hbase_tag_name;
          new_result2.push(t_sql_value);
          k++;
        }
      }
    }

    return new_result2;
  }

  console.log('new_result', new_result);
});

我想结果是

0: Object
  TagName: "a_1"
  hbase_tag_name: "a_12016"
  hbase_tvalue: "1"
  name: "a"
__proto__: Object
1: Object
  TagName: "a_1"
  hbase_tag_name: "a_12016"
  hbase_tvalue: "2"
  name: "a"
__proto__: Object
2: Object
  TagName: "b_1"
  hbase_tag_name: "b_12016"
  hbase_tvalue: "1"
  name: "b"
__proto__: Object
3: Object
  TagName: "b_1"
  hbase_tag_name: "b_12016"
  hbase_tvalue: "3"
  name: "b"

但我总是得到结果

0: Object
  TagName: "a_1"
  hbase_tag_name: "a_12016"
  hbase_tvalue: "2"
  name: "a"
__proto__: Object
1: Object
  TagName: "a_1"
  hbase_tag_name: "a_12016"
  hbase_tvalue: "2"
  name: "a"
__proto__: Object
2: Object
  TagName: "b_1"
  hbase_tag_name: "b_12016"
  hbase_tvalue: "3"
  name: "b"
__proto__: Object
3: Object
  TagName: "b_1"
  hbase_tag_name: "b_12016"
  hbase_tvalue: "3"
  name: "b"

这是我的JSFiddle

1 个答案:

答案 0 :(得分:1)

问题在于,当您实际上只是在下面两条突出显示的行中创建对同一对象的新引用时,您认为您正在克隆一个对象:

  function temp(a, b) {
    var new_result2 = [];
    var k = 0;

    for (var i=0; i<a.length; i++) {
      var sql_value = a[i];                 ////////// ERROR 1
      var tag_name = sql_value.TagName;

      for (var j=0; j<b.length; j++) {
        var hbase_value = b[j];
        var hbase_tag = hbase_value.TagName;
        var hbase_tvalue = '';
        var hbase_tag_name = '';

        console.log('/', hbase_tag + ', ' + tag_name);
        if (hbase_tag.indexOf(tag_name) > -1)
        {
          var t_sql_value = sql_value;      ////////// ERROR 2
          hbase_tvalue = hbase_value.Tvalue;
          hbase_tag_name = hbase_value.TagName;
          t_sql_value.hbase_tvalue = hbase_tvalue;
          t_sql_value.hbase_tag_name = hbase_tag_name;
          new_result2.push(t_sql_value);
          k++;
        }
      }
    }

    return new_result2;
  }

如果您在原始代码运行后检查sqlTable,您会发现它的功能严重受损。

要解决此问题,请通过更改上面的每一行来真正克隆

var sql_value = { name: a[i].name, TagName: a[i].TagName };             // ERROR 1
var t_sql_value = { name: sql_value.name, TagName: sql_value.TagName }; // ERROR 2