gpt4 book ai didi

javascript - 从 map 中减去特定的最后一个单词

转载 作者:行者123 更新时间:2023-12-03 06:39:17 25 4
gpt4 key购买 nike

我有公司名称,我使用下面的代码循环并创建缩写

dataset.nodes.forEach(d => {d.name = d.fullName.split(' ').map(w => w[0]).join('')});

劳埃德银行集团 PLC 是一家公司的示例,我想检查最后一个词是否是 PLC/plc/p.l.c。并在创建缩写之前将其删除,因为现在该公司的缩写将是 LBGP,而我希望它是 LBG

数据:

    var dataset = {"nodes":[{"fullName":"Anglo American plc"},{"fullName":"Whitbread PLC"}],"edges":[{"source":0,"target":29,"officers":["MEDORI, Rene"]},{"source":0,"target":30,"officers":["GROTE, Byron Elmer"]},{"source":0,"target":31,"officers":["GROTE, Byron Elmer"]},{"source":1,"target":13,"officers":["BASON, John George"]},{"source":1,"target":18,"officers":["HAUSER, Wolfhart Gunnar, Dr"]},{"source":1,"target":27,"officers":["SINCLAIR, Charles James Francis"]},{"source":2,"target":14,"officers":["COURT, Annette Elizabeth"]},{"source":3,"target":11,"officers":["GREEN, Andrew James"]},{"source":3,"target":12,"officers":["PUSEY, Stephen Charles"]},{"source":3,"target":15,"officers":["KENNEDY, Christopher John"]},{"source":3,"target":24,"officers":["CHAMBERS, Stuart John"]},{"source":3,"target":31,"officers":["CHAMBERS, Stuart John"]},{"source":3,"target":32,"officers":["KENNEDY, Christopher John"]},{"source":4,"target":8,"officers":["BARKER, Glyn"]},{"source":4,"target":12,"officers":["WHEWAY, Jonathan Scott"]}]};

最佳答案

分割之后,根据正则表达式测试最后一个元素,然后继续处理映射:

omits = /p\.?l\.?c\.?/i;

dataset.nodes.forEach(d => {
const parts = d.fullName.split(' ');
if (omits.test(parts[parts.length-1])) parts.pop();
d.name = parts . map(w => w[0]).join('')});
});

理论上你可以用一个或两个正则表达式来做到这一点:

data.nodes.forEach(d =>
d.name = d.name . replace(omits, '') . match(/\b\w/g) . join('');
);

请注意,我在这里省略了 {},因为它们在技术上不是必需的。

关于javascript - 从 map 中减去特定的最后一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38041160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com