简体   繁体   中英

How to assign a variable which function takes as a parameter

I'm trying to do something like this, but variable is not 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;
}

Here's i'm trying to call this function

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;
}

Maybe you forgot to set a return on the final of the function:

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;
}

Did you try to do this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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