为什么我的Google JavaScript数组映射删除值?

发布于 2025-01-20 00:41:16 字数 1714 浏览 3 评论 0原文

我有一个要在谷歌表格中处理的数组问题。我正在尝试将一个值从一张纸映射到另一张纸上。感谢您的任何帮助。我在下面有解释和代码...

问题:这给了我空白行(policies2[i])。

期望的结果:policies2 最后一行中 entry_top 的新值。

我正在使用两张纸。 policies.values 是来自普通 Google 表格值表的数组。 policies2policies 的副本。 entry_top 是包含非结构化数据的工作表中的值数组,但标题和值彼此相邻。

因此,如果“Line”是 policies 中的标头,它将在 entry_top 的单元格/数组节点中找到“Line”,然后获取下一个单元格/数组值(下一个)列/索引/右侧),获取该值并将其放入 policies 中同一列的最后一行(按标题匹配)。

因此,下面的代码循环遍历 policies.values[0](标头值)。然后按行 (e) 和列 (ee) 循环遍历 entry_top 中的每个单元格。然后,如果标头匹配(hee),则会将下一个值 ([ei][eei+1]) 放入匹配的内容中最后一行的 policies2 列 ([policies2.length-1][hi])。

policies.values[0].map((h, hi) => {
  entry_top.map((e, ei) => {
    e.map((ee, eei) => {
      if (ee == h) policies2[policies2.length - 1][hi] = entry_top[ei][eei + 1];
    });
  });
});

MRE:奇怪的是这个例子有效。所以不确定上面是什么导致了问题......

function testdp() {

  policies = {
    values:[[1,2,3],[4,5,6],[7,8,9]]
  }
  policies2=[[1,2,3],[4,5,6],[7,8,9],[,,,]];
  entry_top = [[,,1,'add',,],[,'a','b',2,'add2',,'c'],['c',,,3,'add3',,]]

  Logger.log(policies.values);
  Logger.log(policies2);
  Logger.log(entry_top);
  policies.values[0].forEach((h,hi)=>{
    entry_top.forEach((e,ei)=>{
      e.forEach((ee,eei)=>{
      if (ee == h) policies2[policies2.length - 1][hi] = entry_top[ei][eei + 1];
      })
    })   
  });
  
  Logger.log(policies2);
// [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [add, add2, add3]]

}

I've got an array question for processing in google sheets. I'm trying to map a value from one sheet into another. Thanks for any help. I've got explanation and code below...

PROBLEM: this gives me blank rows (policies2[i]).

DESIRED OUTCOME: new values from entry_top in the last row of policies2.

I'm working with two sheets. policies.values is the array from a normal google sheet of tabular values. policies2 is a copy of policies. entry_top is the array of values from a sheet with unstructured data, except that headers and values are beside each other.

So if "Line" is a header in policies, it would find "Line" in a cell/array node in entry_top and then get the next cell/array value (next column/index/to the right), take that value and put it in the last row in the same column in policies (match by header).

So the code below loops through policies.values[0], the header values. Then loops through every cell in entry_top, by rows (e) and columns (ee). Then if the headers match (h and ee), it puts the next value ([ei][eei+1]) in the matching policies2 column in the last row ([policies2.length-1][hi]).

policies.values[0].map((h, hi) => {
  entry_top.map((e, ei) => {
    e.map((ee, eei) => {
      if (ee == h) policies2[policies2.length - 1][hi] = entry_top[ei][eei + 1];
    });
  });
});

MRE: oddly this example works. so not sure what above is causing an issue...

function testdp() {

  policies = {
    values:[[1,2,3],[4,5,6],[7,8,9]]
  }
  policies2=[[1,2,3],[4,5,6],[7,8,9],[,,,]];
  entry_top = [[,,1,'add',,],[,'a','b',2,'add2',,'c'],['c',,,3,'add3',,]]

  Logger.log(policies.values);
  Logger.log(policies2);
  Logger.log(entry_top);
  policies.values[0].forEach((h,hi)=>{
    entry_top.forEach((e,ei)=>{
      e.forEach((ee,eei)=>{
      if (ee == h) policies2[policies2.length - 1][hi] = entry_top[ei][eei + 1];
      })
    })   
  });
  
  Logger.log(policies2);
// [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [add, add2, add3]]

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情未る 2025-01-27 00:41:16

这一切都很好。我记录了错误的结果。

this all works just fine. I was logging the wrong result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文