javascript 比较2个行列表值和替换值时出错

gajydyqb  于 2023-09-29  发布在  Java
关注(0)|答案(1)|浏览(83)

下面是我创建的一个代码,试图比较两个行列表的值,并将值替换为第三个列表的行。但它并没有产生我所期望的结果!

//liste compléte
const lArr = [ 
    ['ALPINES MUSEUM DAV','0','0','0','0'],
    ['ALPINES MUSEUM DAV','0','0','0','0'],
    ['ARROMANCHES','0','0','0','0'],
    ['BELVEDERE MUSEUM','0','0','0','0'],
    ['BELVEDERE MUSEUM','0','0','0','0'],
    ['BELVEDERE MUSEUM','0','0','0','0'],
    ['BELVEDERE MUSEUM','0','0','0','0'],
    ['BELVEDERE MUSEUM','0','0','0','0'],
    ['BERGBAUMUSEUM OELSNITZ','0','0','0','0'],
    ['BERGBAUMUSEUM OELSNITZ','0','0','0','0']
  ];
//liste des config autorisées
const iArr = [ 
    ['ALPINES MUSEUM DAV','0','0','0','0'],
    ['ARROMANCHES','0','0','0','0'],
    ['BERGBAUMUSEUM OELSNITZ','0','0','0','0'],
  ];
//list des choses qui remplacent
const iArr2 = [ 
     ['ALPINES MUSEUM DAV, 2D, "MAREIKE", 50, 33.0'],
     ['ALPINES MUSEUM DAV, PROJECT MANAGEMENT, SOLENN, 50, 23.5'],
     ['ARROMANCHES, PROJECT MANAGEMENT, NICOLAS, 50, 6.0'],
     ['BELVEDERE MUSEUM, FABRICATION, CAMILLE, 50, 6.0'],
     ['BELVEDERE MUSEUM, PROJECT MANAGEMENT, ALEXANDRA, 50, 2.0'],
     ['BELVEDERE MUSEUM, PROJECT MANAGEMENT, LOUISE, 50, 3.75'],
     ['BELVEDERE MUSEUM, 2D, JULIEN, 50, 31.5'],
     ['BELVEDERE MUSEUM, 2D, MARTINA, 50, 4.0'],
     ['BERGBAUMUSEUM OELSNITZ, FABRICATION, ZELIE, 50, 4.0'],
     ['BERGBAUMUSEUM OELSNITZ, FABRICATION, CAMILLE, 50, 4.5']
     ];

const out = lArr.map(
  (lRow) =>
iArr2[iArr.findIndex((iRow) => iRow.every((el, i) => el === lRow[i]))] ?? ['NONE','NONE','NONE','NONE', 'NONE']
);

console.log(out);

因此,console.log(out);中的结果应该是:

[ 
         ['ALPINES MUSEUM DAV, 2D, "MAREIKE", 50, 33.0'],
         ['ALPINES MUSEUM DAV, PROJECT MANAGEMENT, SOLENN, 50, 23.5'],
         ['ARROMANCHES, PROJECT MANAGEMENT, NICOLAS, 50, 6.0'],
         ['NONE','NONE','NONE','NONE', 'NONE'],
         ['NONE','NONE','NONE','NONE', 'NONE'],
         ['NONE','NONE','NONE','NONE', 'NONE'],
         ['NONE','NONE','NONE','NONE', 'NONE'],
         ['NONE','NONE','NONE','NONE', 'NONE'],
         ['BERGBAUMUSEUM OELSNITZ, FABRICATION, ZELIE, 50, 4.0'],
         ['BERGBAUMUSEUM OELSNITZ, FABRICATION, CAMILLE, 50, 4.5']
         ]

但事实并非如此,因为代码似乎有点跳行并覆盖了一些东西……
我的代码是基于这个one的,但是我还没有能够理解它的任何东西,我所知道的是,他完全适合另一个主题的情况,而不是这里的特定情况。
我也试着在这个page上捕捉一些信息,但是即使它让我理解了一些东西,它也没有真正帮助我找到解决方案...
我猜这段代码可能可以通过小的修改来工作,但是如果你有一个完全不同的建议来做这项工作,我会很感激的!
谢谢你的帮助!

mznpcxlj

mznpcxlj1#

虽然我不确定我是否能正确理解您期望的结果,但下面的示例脚本如何?

示例脚本:

//liste compléte
const lArr = [
  ['ALPINES MUSEUM DAV', '0', '0', '0', '0'],
  ['ALPINES MUSEUM DAV', '0', '0', '0', '0'],
  ['ARROMANCHES', '0', '0', '0', '0'],
  ['BELVEDERE MUSEUM', '0', '0', '0', '0'],
  ['BELVEDERE MUSEUM', '0', '0', '0', '0'],
  ['BELVEDERE MUSEUM', '0', '0', '0', '0'],
  ['BELVEDERE MUSEUM', '0', '0', '0', '0'],
  ['BELVEDERE MUSEUM', '0', '0', '0', '0'],
  ['BERGBAUMUSEUM OELSNITZ', '0', '0', '0', '0'],
  ['BERGBAUMUSEUM OELSNITZ', '0', '0', '0', '0']
];
//liste des config autorisées
const iArr = [
  ['ALPINES MUSEUM DAV', '0', '0', '0', '0'],
  ['ARROMANCHES', '0', '0', '0', '0'],
  ['BERGBAUMUSEUM OELSNITZ', '0', '0', '0', '0'],
];
//list des choses qui remplacent
const iArr2 = [
  ['ALPINES MUSEUM DAV, 2D, "MAREIKE", 50, 33.0'],
  ['ALPINES MUSEUM DAV, PROJECT MANAGEMENT, SOLENN, 50, 23.5'],
  ['ARROMANCHES, PROJECT MANAGEMENT, NICOLAS, 50, 6.0'],
  ['BELVEDERE MUSEUM, FABRICATION, CAMILLE, 50, 6.0'],
  ['BELVEDERE MUSEUM, PROJECT MANAGEMENT, ALEXANDRA, 50, 2.0'],
  ['BELVEDERE MUSEUM, PROJECT MANAGEMENT, LOUISE, 50, 3.75'],
  ['BELVEDERE MUSEUM, 2D, JULIEN, 50, 31.5'],
  ['BELVEDERE MUSEUM, 2D, MARTINA, 50, 4.0'],
  ['BERGBAUMUSEUM OELSNITZ, FABRICATION, ZELIE, 50, 4.0'],
  ['BERGBAUMUSEUM OELSNITZ, FABRICATION, CAMILLE, 50, 4.5']
];

const out = lArr.map((r, i) => iArr.some(rr => rr.join("") == r.join("")) ? iArr2[i] : ['NONE', 'NONE', 'NONE', 'NONE', 'NONE']);

console.log(out);
  • 运行此脚本时,将获得以下结果。
[
  ["ALPINES MUSEUM DAV, 2D, \"MAREIKE\", 50, 33.0"],
  ["ALPINES MUSEUM DAV, PROJECT MANAGEMENT, SOLENN, 50, 23.5"],
  ["ARROMANCHES, PROJECT MANAGEMENT, NICOLAS, 50, 6.0"],
  ["NONE","NONE","NONE","NONE","NONE"],
  ["NONE","NONE","NONE","NONE","NONE"],
  ["NONE","NONE","NONE","NONE","NONE"],
  ["NONE","NONE","NONE","NONE","NONE"],
  ["NONE","NONE","NONE","NONE","NONE"],
  ["BERGBAUMUSEUM OELSNITZ, FABRICATION, ZELIE, 50, 4.0"],
  ["BERGBAUMUSEUM OELSNITZ, FABRICATION, CAMILLE, 50, 4.5"]
]

参考资料:

相关问题