繁体   English   中英

如何使用 Javascript 根据条件/索引替换内部数组的元素?

[英]How to replace elements of an inner array on condition/index basis using Javascript?

我已经看到了类似问题的一些答案,但我还没有看到一个涉及多个条件的答案,我无法适应这种需要。

基本上, ar1应该将其标签替换为 url。

这是 function 打我:

 let ar1 = [ ["Client", "Date", "Label1", "Label2"], ["A", "11/15/2022", "Label1", "Label2"], ["B", "11/15/2022", "Label1", "Label2"], ["C", "11/15/2022", "Label1", "Label2"] ]; const label1Col = ar1[0].indexOf('Label1') const label2Col = ar1[0].indexOf('Label2') let ar2 = [ ["Client", "Date", "Label2", "Label1",], ["A", "11/15/2022", "www.elonscrewing.com", "www.google.com"], ["B", "11/15/2022", "www.elonscrewing.com", "www.google.com"], ["C", "11/15/2022", "www.elonscrewing.com", "www.google.com"] ] const ar2label1Col = ar2[0].indexOf('Label1') const ar2label2Col = ar2[0].indexOf('Label2') ar1.forEach(function(val) { ar2.forEach(function(link) { val[label1Col] = link[ar2label1Col]; val[label2Col] = link[ar2label2Col]; }) }) console.log(ar1)

感谢任何帮助!

ar2 中的错字。 特别是 ar2[0][2] 是 labe2 而不是 label2(:

你在ar2中有错字

[“客户”,“日期”,“Labe2”,“Label1”,]

缺少 Label2 中的L

您真的应该检查拼写错误 - 在 ar2 中包含“Labe2”会使 indexOf(“Label2”) 返回 -1。

顺便说一句。 第二个循环是无用的(当您的链接不完全相同时很可能会导致错误)

下面是固定代码:

 let ar1 = [ ["Client", "Date", "Label1", "Label2"], ["A", "11/15/2022", "Label1", "Label2"], ["B", "11/15/2022", "Label1", "Label2"], ["C", "11/15/2022", "Label1", "Label2"] ]; const label1Col = ar1[0].indexOf('Label1') const label2Col = ar1[0].indexOf('Label2') let ar2 = [ ["Client", "Date", "Label2", "Label1",], ["A", "11/15/2022", "www.elonscrewing.com", "www.google.com"], ["B", "11/15/2022", "www.elonscrewing.com", "www.google.com"], ["C", "11/15/2022", "www.elonscrewing.com", "www.google.com"] ] const ar2label1Col = ar2[0].indexOf('Label1') const ar2label2Col = ar2[0].indexOf('Label2') console.log(ar2label2Col) //this line is what you should have done in order to debug (this line caught the problem / typo) ar1.forEach(function(val, index) { val[label1Col] = ar2[index][ar2label1Col]; val[label2Col] = ar2[index][ar2label2Col]; }) console.log(ar1)

对于无序的 arrays(/具有不同顺序的数组)只需检查 if 条件是否 Date 和 Client 相等(并将条件与 &&s 串在一起) - 请参见下面的代码:

但那时你真的应该考虑一下,如果 arrays 仍然是你想要使用的数据类型。 这对于对象来说会更容易(所以如果你经常做类似的操作,可以考虑将你的 arrays 转换为对象(让生活更轻松,代码更易读)

 let ar1 = [ ["Client", "Date", "Label1", "Label2"], ["B", "11/15/2022", "Label1", "Label2"], ["A", "11/15/2022", "Label1", "Label2"], ["C", "11/15/2022", "Label1", "Label2"] ]; const label1Col = ar1[0].indexOf('Label1') const label2Col = ar1[0].indexOf('Label2') const dateIdx = ar1[0].indexOf('Date') const clientIdx = ar1[0].indexOf('Client') let ar2 = [ ["Client", "Date", "Label2", "Label1",], ["A", "11/15/2022", "www.aaaaaaaaaaa.com", "www.google.com"], ["B", "11/15/2022", "www.bbbbbbbbbb.com", "www.google.com"], ["C", "11/15/2022", "www.elonscrewing.com", "www.google.com"] ] const ar2label1Col = ar2[0].indexOf('Label1') const ar2label2Col = ar2[0].indexOf('Label2') const ar2ClientIdx = ar2[0].indexOf('Client') const ar2DateIdx = ar2[0].indexOf('Date') ar1.forEach(function(val) { ar2.forEach(function(link) { if (val[clientIdx] === link[ar2ClientIdx] && val[dateIdx] === link[ar2DateIdx]) { val[label1Col] = link[ar2label1Col]; val[label2Col] = link[ar2label2Col]; } }) }) console.log(ar1)

暂无
暂无

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

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