NodeJS 如何将JS中的值从一个函数更改为另一个函数

uqjltbpv  于 2023-02-21  发布在  Node.js
关注(0)|答案(2)|浏览(110)

我这里有一个数组,有一个值和一个字符串,我需要的是,如果数组中的任何一个值,在字符串中得到匹配,它应该用空替换匹配的字符串,现在我在函数中做这个,我假设回调值,但我不知道我做错了什么。
附加以下代码

async function ok (){

var arr = ['one', 'two', 'three'];
var addressLine = 'This is one'

const ans = await matchAndRemove(arr, addressLine)

console.log(`This is address ${addressLine}`) 
// Out put is 'This is address This is one' but here I expect the output as 'This is'
console.log(`This is match ${ans}`) // Output is 'This is match one'
}

const matchAndRemove = (arr, addressLine) => {

    for (var val of arr) {
        if (addressLine.toLowerCase().indexOf(val.toLowerCase()) != -1) {
            const match = addressLine.match(new RegExp(val, 'ig')).join(' ');
            addressLine = addressLine.replace(new RegExp(val, 'ig'), '');
            console.log(addressLine) // Output is 'This is'
            return match;
        }
    }

}

ok()

有什么建议来解决这个问题吗?

jhdbpxl9

jhdbpxl91#

你的函数不能修改addressLine,因为JavaScript没有引用调用(而且字符串是不可变的)。如果你想修改那个字符串的值,你也可以返回它。
因此,返回addressLine的匹配值和新值,您可以将其作为一个对(数组)返回,调用者可以将该对重新构造为单独的变量。
注意:不需要await,因为不涉及异步API
更正:

function ok (){
    var arr = ['one', 'two', 'three'];
    var addressLine = 'This is one'

    // Get two returns by destructuring
    const [newAddressLine, ans] = matchAndRemove(arr, addressLine)

    console.log("newAddressLine:", newAddressLine) // 'This is'
    console.log("ans:", ans) // 'one'
}

const matchAndRemove = (arr, addressLine) => {
    for (var val of arr) {
        if (addressLine.toLowerCase().indexOf(val.toLowerCase()) != -1) {
            const match = addressLine.match(new RegExp(val, 'ig')).join(' ');
            addressLine = addressLine.replace(new RegExp(val, 'ig'), '');
            return [addressLine, match]; // Return both as a pair
        }
    }
}

ok()

这不是您的问题,但使用 * one * 正则表达式可能更有趣,而不是需要逐个检查的单词数组:

const arr = ['one', 'two', 'three'];
const regex = RegExp("\\b(" + arr.join("|") + ")\\b\\s*", "gi"); // One regex for all

var addressLine = 'This is one';

// One replace call will replace all words in arr
const result = addressLine.replace(regex, "").trim();
console.log("result:", result) // 'This is'

// If you wanted to know the matches:
const matched = addressLine.match(regex);
console.log("matched:", matched);
jaxagkaj

jaxagkaj2#

您的代码在for循环中返回match的值。这就是为什么找到第一个匹配项后函数会关闭的原因。您应该将匹配项存储在数组中,然后将它们连接起来以获得最终结果。请尝试以下操作:

async function ok() {
  var arr = ['one', 'two', 'three'];
  var addressLine = 'This is one';

  const ans = await matchAndRemove(arr, addressLine);

  console.log(`This is address ${addressLine}`); // Output is 'This is'
  console.log(`This is match ${ans}`); // Output is 'This is match one'
}

const matchAndRemove = (arr, addressLine) => {
  let matches = [];

  for (var val of arr) {
    if (addressLine.toLowerCase().indexOf(val.toLowerCase()) != -1) {
      matches.push(val);
      addressLine = addressLine.replace(new RegExp(val, 'ig'), '');
    }
  }

  return matches.join(' ');
};

ok();

相关问题