简体   繁体   English

我想将具有相同变量名的 JavaScript 对象组合起来

[英]I want to combine JavaScript Objects with the same variable name

Right now, If i run for loop现在,如果我运行 for 循环

{“FirstName” : “Kim”}
{“LastName” : “hana”}
{“Phone” : “010-1234-5648”}
{“Email” : “abc@gmail.com”}

It comes out like this.它是这样出来的。 But the result I want is但我想要的结果是

{“FirstName” : “Kim”, “LastName” : “hana”, “Phone” : “010-1234-5648”, “Email” : “abc@gmail.com”}

I want to make it come out like this.我想让它像这样出来。 What should I do?我应该怎么办?

This is my code that I just wrote.这是我刚刚写的代码。

function testRun(){
    var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var LastRow = ss.getLastRow();
    var LastCol = ss.getLastColumn();

    var arr = [];
    for(var i = 1; i<=LastCol; i ++){
        var fieldName = ss.getRange(1,i).getValue();
        arr.push(fieldName);
    }

    //arr = [FirstName, LastName, Phone, Email]

    for(var i =2;i<=LastRow;i++){
        for(var j=1;j<=LastCol;j++){
            var payload =  Utilities.jsonStringify({
              [arr[j-1]] : ss.getRange(i,j).getValue()}
            );
        }
    }
 }

Modification points:修改点:

  • In your showing script, it seems that payload is not used.在您的显示脚本中,似乎未使用payload
  • When getValue() is used in a loop, the process cost becomes high.在循环中使用getValue()时,处理成本变高。 Ref 参考

When these points are reflected in a sample script for achieving your goal, it becomes as follows.当这些点反映在实现目标的示例脚本中时,它变成如下。

Sample script:示例脚本:

When your showing script is modified, how about the following modification?当你的放映脚本被修改时,下面的修改怎么样?

function testRun() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var LastRow = ss.getLastRow();
  var LastCol = ss.getLastColumn();
  var [header, ...values] = ss.getRange(1, 1, LastRow, LastCol).getValues();
  var arr = [];
  for (var i = 0; i < LastRow - 1; i++) {
    var temp = {};
    for (var j = 0; j < LastCol; j++) {
      temp[header[j]] = values[i][j];
    }
    arr.push(temp);
  }
  console.log(JSON.stringify(arr))
}
  • When this script is run, arr is [{"FirstName":"Kim","LastName":"hana","Phone":"010-1234-5648","Email":"abc@gmail.com"}] .运行此脚本时, arr[{"FirstName":"Kim","LastName":"hana","Phone":"010-1234-5648","Email":"abc@gmail.com"}] .

  • As another apporach, I thought that you might be able to also use the following sample script.作为另一种方法,我认为您也可以使用以下示例脚本。

     var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var [header, ...values] = ss.getRange(1, 1, ss.getLastRow(), ss.getLastColumn()).getValues(); var arr = values.map(r => header.reduce((o, h, j) => (o[h] = r[j], o), {})); console.log(JSON.stringify(arr))

Your code creates separate objects in each iteration.您的代码在每次迭代中创建单独的对象。 Create an empty object for every Person, then add the properties you need.为每个人创建一个空的 object,然后添加您需要的属性。

This should look something like this:这应该看起来像这样:

for(var i=2; i<=LastRow; i++){
    var payload = {}
    for(var j=1; j<=LastCol; j++){
        payload[arr[j-1]] = ss.getRange(i,j).getValue()};
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM