首页 > 解决方案 > 函数输出替换了我的主要 JSON 字符串的前两行

问题描述

我有一个 JSON 查询,我正在使用console.log它来展示它:

var json_data = {"headers":["Month","Value","Number"],"rows":[["2018-10-01 00:00:00.0","one",209],["2018-09-01 00:00:00.0","one",274],["2018-09-01 00:00:00.0","five",183],["2018-10-01 00:00:00.0","five",164],["2018-09-01 00:00:00.0","four",214],["2018-10-01 00:00:00.0","four",192],["2018-09-01 00:00:00.0","three",128],["2018-10-01 00:00:00.0","three",125],["2018-09-01 00:00:00.0","two",199],["2018-10-01 00:00:00.0","two",169],["2018-09-01 00:00:00.0","seven",10541],["2018-10-01 00:00:00.0","seven",6139],["2018-10-01 00:00:00.0","six",169],["2018-09-01 00:00:00.0","six",233]]};

document.getElementById("original").innerHTML = json_data.rows;
 
<div style="background:yellow; "id="original"></div>
<div style="background:red;" id="output"></div>

对于值“一”,我有两个数字(209274)。

然后我使用一个函数来分组,它工作正常(输出)。我的问题是,当我使用console.log初始 json_data 查询时,前两行是不同的。看来我的函数用输出的行(红色)替换了前两行。函数在这里给出:

    function initialGroupBy(rows) {
  const 
    rowMap = new Map(),
    result = [],
    dataTemp = [];

  // Iterate over the rows.
  rows.forEach(row => {

    const
      // Create a key, it is the first elements joined together.
      key = row.slice(0,1).join();

    // Check if the Map has the generated key...
    if (rowMap.has(key)) {
      // The map has the key, we need to add up the values
      const
        // Get the value for the current key.
        storedRow = rowMap.get(key);
        // Add the value of the current row to the row in the map.
        storedRow[2] += row[2];

    } else {
      // The key doens't exist yet, add the row to the map.
      rowMap.set(key, row);
    }

  });

  // Iterate over all the entries in the map and push each value with the
  // summed up value into the array.
  rowMap.forEach(value => {
    result.push(value);
  });


    for (i = 0; i < result.length; i++) 
    {
    var object2 = {"date": result[i][0].slice(0,7), "num": result[i][2]};
    dataTemp.push(object2);      
    }

    return dataTemp;

}

可以在此处找到完整的片段(比较两个片段中黄色框的前两行):

var json_data = {"headers":["Month","Value","Number"],"rows":[["2018-10-01 00:00:00.0","one",209],["2018-09-01 00:00:00.0","one",274],["2018-09-01 00:00:00.0","five",183],["2018-10-01 00:00:00.0","five",164],["2018-09-01 00:00:00.0","four",214],["2018-10-01 00:00:00.0","four",192],["2018-09-01 00:00:00.0","three",128],["2018-10-01 00:00:00.0","three",125],["2018-09-01 00:00:00.0","two",199],["2018-10-01 00:00:00.0","two",169],["2018-09-01 00:00:00.0","seven",10541],["2018-10-01 00:00:00.0","seven",6139],["2018-10-01 00:00:00.0","six",169],["2018-09-01 00:00:00.0","six",233]]};

function initialGroupBy(rows) {
  const 
    rowMap = new Map(),
    result = [],
    dataTemp = [];
    
  // Iterate over the rows.
  rows.forEach(row => {
  
    const
      // Create a key, it is the first elements joined together.
      key = row.slice(0,1).join();
      
    // Check if the Map has the generated key...
    if (rowMap.has(key)) {
      // The map has the key, we need to add up the values
      const
        // Get the value for the current key.
        storedRow = rowMap.get(key);
        // Add the value of the current row to the row in the map.
        storedRow[2] += row[2];
        
    } else {
      // The key doens't exist yet, add the row to the map.
      rowMap.set(key, row);
    }

  });
  
  // Iterate over all the entries in the map and push each value with the
  // summed up value into the array.
  rowMap.forEach(value => {
    result.push(value);
  });
    

	for (i = 0; i < result.length; i++) 
	{
    var object2 = {"date": result[i][0].slice(0,7), "num": result[i][2]};
    dataTemp.push(object2);      
	}

	return dataTemp;

}

const damn = initialGroupBy(json_data.rows);

 
document.getElementById("original").innerHTML = json_data.rows;
document.getElementById("output").innerHTML =JSON.stringify(damn);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div style="background:yellow; "id="original"></div>
<br><br>
<div style="background:red;" id="output"></div>

在很多情况下,我都尝试过将其更改为varconst我在这里错过了一个基本JavaScript案例吗?

标签: javascriptarraysjsongroup-by

解决方案


A) const 声明创建对值的只读引用。这并不意味着它持有的价值是不可变的-> Link

B)您的问题是您实际上是在 initialGroupBy 函数中编辑原始对象。也许这个答案 会有所帮助。


推荐阅读