代码之家  ›  专栏  ›  技术社区  ›  Datacrawler

函数输出替换主JSON字符串的前两行

  •  0
  • Datacrawler  · 技术社区  · 6 年前

    我有一个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>

    “一个” 我有两个号码( ).

    然后我使用一个函数来groupby,它可以正常工作(输出)。我的问题是当我使用 控制台.log 对于初始的json\u数据查询,前两行是不同的。似乎我的函数用输出的行(红色)替换了前两行。函数如下:

        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>

    我试过换衣服 var const JavaScript 这里有箱子吗?

    3 回复  |  直到 6 年前
        1
  •  0
  •   Datacrawler    6 年前

    作为你的 价值 是数组对象,当您将其保存在临时 地图

    所以在一开始 forEach公司 循环,对实际更改原始数组项的值求和。

    解决方案非常简单,只需克隆阵列:

    rowMap.set(key, row.slice());
    

    另一种可能是使用不同的数组来保存总数。

    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.slice());
        }
    
      });
      
      // 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>
    <div style="background:red;" id="output"></div>
        2
  •  0
  •   Giorgos Papageorgiou    6 年前

    Link

    B) 您的问题是,您实际上是在编辑initialGroupBy函数中的原始对象。也许是这个 answer

        3
  •  0
  •   Datacrawler    6 年前

    这里应用了不同的逻辑,结果很方便:

    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 groupBy(accumulator, item) {
    const [date,extra,value] = item;
    
    const key = date.slice(0,7);
    
    if(!accumulator[key]){
    accumulator[key] = 0
    }
    
    accumulator[key] += value;
    return accumulator;
    
    }
    
    var damn = json_data.rows.reduce(groupBy,{});
    
    damn = Object.keys(damn).map(function(key){
    return {date: key, Value: "Total", num: damn[key]};
    })
    
    document.getElementById("original").innerHTML = json_data.rows;
    document.getElementById("output").innerHTML =JSON.stringify(damn);
    <div style="background:yellow; "id="original"></div>
    <div style="background:red;" id="output"></div>