繁体   English   中英

如何分配以函数为参数的变量

[英]How to assign a variable which function takes as a parameter

我正在尝试做这样的事情,但变量不是Assign(

const insert = (str, index, pasteString) => {
  let res;
  if (index > 0) {
    res = str.substring(0, index) + pasteString + str.substring(index, str.length);
  } else {
    res = pasteString + str;
  }
  str = res;
}

这是我正在尝试调用此功能

const filterDescription = (obj) => {
  const str = obj.description;
  const strLen = str.length;
  const fifty = Math.ceil(regexIndexOf(/[\/.!;?]/, str, strLen / 2));
  const oneOfFour = Math.ceil(regexIndexOf(/[\/.!;?]/, str, strLen / 4));
  const oneOfThree = Math.ceil(regexIndexOf(/[\/.!;?]/, str, strLen / 3));

  console.log([fifty, oneOfFour, oneOfThree]);
  insert(str, fifty, `string`);
  insert(str, oneOfFour, `string`);
  insert(str, oneOfThree, `string`);
  insert(str, 1, `string`);
  insert(str, strLen - 1, `string`);
  return str;
}

也许您忘了在函数的最后设置返回值:

const insert = (str, index, pasteString) => {
    let res;
    if (index > 0) {
        res = str.substring(0, index) + pasteString + str.substring(index, str.length);
    } else {
        res = pasteString + str;
    }
    return res;
}

您尝试这样做吗?

您可以传递变量指针,或者更好地说,通过引用该函数传递str变量并对其进行更新。

暂无
暂无

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

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