我正在创建一个基于网络的会计应用程序,供高中生用作练习。我的transactionListArray
包含我的所有交易,这些交易都是在JS代码的幕后随机生成的。 transactionListArray
包含某些字符,包括第一个字符,即date
作为integer
,后跟.
(例如:10.
或{{1 }}等)。日期之后有一个句子,用于创建会计交易的措词,帐户名称,付款方式以及其他各种内容。
一个基本事务产生以下输出:
12.
我到处都是,但仍然找不到适合自己喜好的解决方案。
我几天来一直面临的问题是试图弄清楚如何使用正则表达式27. Trusted Traders purchased trading stock to the value of R108756.
关键字返回字符串。当我尝试将match
与currentString
(即数组中的下一个值)匹配时,就会出现问题。
请参见下文:
nextString
以上内容不会产生我期望的输出。 let length = array.length-1;
for (let i = 0; i < length; i++) {
let regex = /d+\./; // this returns the value of the first number(the date) and the "." symbol
let currentString = array[i];
let nextString = array[i+1];
let currentDate = currentString.match(regex); // errors
let nextDate = nextString.match(regex); // errors
};
和currentDate
行指出的错误是:
nextDate
这个问题令人困惑,因为我已经检查了当前迭代和下一个迭代的值,但是它没有返回我的正则表达式字符串。
例如,我期望这样:
TypeError: Cannot read property '0' of null
然后,我想在currentDate[0] = '11.';
nextDate[0] = '10.';
和nextString
相等时替换currentString
。
像这样:
NextString
我在let replaceDateWithBlankSpace = (array) => {
let length = array.length-1;
for (let i = 0; i < length; i++) {
let regex = /d+\./;
let currentString = array[i];
let nextString = array[i+1];
let currentDate = currentString.match(regex); // TypeError: Cannot read property '0' of null
let nextDate = nextString.match(regex); // TypeError: Cannot read property '0' of null
if (currentDate[0] === nextDate[0]) { // checking if both are equal
nextString.replace(regex, " "); // and then replacing the string regex that was calculated with blank space at array[i+1]
}
}
};
上这样调用函数:
transactionListArray
答案 0 :(得分:2)
如果您想对原始数组进行变异,则可以这样操作:
const arr = [
'25. Trusted Traders purchased trading stock to the value of R138756.',
'26. Trusted Traders purchased trading stock to the value of R432756.',
'26. Trusted Traders purchased trading stock to the value of R108756.',
'28. Trusted Traders purchased trading stock to the value of R333756.',
];
const replaceDateWithBlankSpace = array => {
const length = array.length - 1;
const regex = /^\d+\./;
for (let i = 0; i < length; i++) {
const currentString = array[i];
const nextString = array[i + 1];
const currentDate = currentString.match(regex);
const nextDate = nextString.match(regex);
if (currentDate && nextDate && currentDate[0] === nextDate[0]) {
array[i + 1] = array[i + 1].replace(regex, ' ');
}
}
};
replaceDateWithBlankSpace(arr);
console.log(arr);
答案 1 :(得分:-1)
好吧,我可能误会了您,因此请检查此解决方案是否符合您的需求:
// I define the transactions array that you generate randomly:
const transactionListArray = [
'5. Lorem ipsum dolor sit amet',
'5. consectetur adipiscing elit',
'6. Praesent aliquet ut ex eget mattis',
'7. Donec fermentum sodales quam',
'7. quis vestibulum justo posuere quis',
'8. Fusce tristique accumsan pretium',
'9. Quisque ante metus',
'9. vestibulum sed lacinia sed',
'9. elementum ac nunc',
'10. Suspendisse vel sollicitudin neque',
'10. vel sagittis quam'
];
function replaceDateWithBlankSpace (array) {
return array.map((transaction, index, array) => {
const regex = /^(\d+\.)(.*)$/;
let lastDate = array[index-1] ? array[index-1].replace(regex, '$1') : '';
let thisDate = transaction.replace(regex, '$1');
if (lastDate === thisDate) {
return transaction.replace(regex, '$1').replace(/./g,' ')
+ transaction.replace(regex, '$2');
} else {
return transaction;
}
});
}
console.log(replaceDateWithBlankSpace(transactionListArray));