简体   繁体   English

为什么即使我已将数字转换回字符串,我也不能将数字推送到我的字符串数组中?

[英]Why can I not push a number into my array of strings even though I have converted the number back to a string?

Hello there I am trying to do a code wars kata: https://www.codewars.com/kata/54a91a4883a7de5d7800009c/train/javascript您好,我正在尝试进行代码大战 kata: https://www.codewars.com/kata/54a91a4883a7de5d7800009c/train/javascript

I know what I have done is very long-winded but I wanted to do it step by step before I refactored it.我知道我所做的事情很冗长,但我想在重构之前一步一步地做。

  1. I have split the string input from the user我已经拆分了用户输入的字符串
  2. Filtered through the array just to get the number and converted it to a NUMBER通过数组过滤只是为了获取数字并将其转换为 NUMBER
  3. Incremented that number by 1将该数字增加 1
  4. Changed number back to string将数字改回字符串
  5. Now I want to add my string number to the original array but it doesn't work and I don't understand why:S现在我想将我的字符串编号添加到原始数组中,但它不起作用,我不明白为什么:S

I know this is not the final answer to the kata but I am just trying things and wondered why this did not work...我知道这不是对 kata 的最终答案,但我只是在尝试并想知道为什么这不起作用......

function isNumeric(num){
 return !isNaN(num)
}

function incrementString (string) {
  const splitString = string.split("");

  let numbers = Number(splitString.filter(el => isNumeric(el)).join("")); //[
  'f', 'o', 'o', 'b',
  'a', 'r', '0', '0',
  '4', '2'
]

  let incrementNumber = numbers +=1; // 43

  let revertNumberToString = incrementNumber.toString(); // "43"

  let test = splitString.push(revertNumberToString); // why can I not push the number 43 onto my original array?

  console.log(test); // 11? why?

}

incrementString("foobar0042")

It does seem to be working correctly.它似乎工作正常。 If you check splitString again after you push to it then it will have all 11 items.如果您在 push 后再次检查 splitString,那么它将包含所有 11 个项目。 That is where the number 11 is coming from.这就是数字 11 的来源。 When you save a push to a variable it doesn't make a new array but rather it saves the length of the new array.当您将推送保存到变量时,它不会创建新数组,而是保存新数组的长度。

  console.log(splitString)
  // ["f", "o", "o", "b", "a", "r", "0", "0", "4", "2"]
  let test = splitString.push(revertNumberToString);
  console.log(splitString)
  // ["f", "o", "o", "b", "a", "r", "0", "0", "4", "2", 43]
  console.log(test); // 11? why?

Javascript push method adds the element to the array and returns the length, that's why you get 11 instead of the array itself. Javascript push 方法将元素添加到数组并返回长度,这就是为什么你得到 11 而不是数组本身。 Reference 参考

You could take a different approach by splitting the value into string and number part and take the length of the number part for later padding the value with leading zeroes.您可以采用不同的方法,将值拆分为字符串和数字部分,并获取数字部分的长度,以便稍后用前导零填充值。

 function incrementString(value) { const string = (value.match(/\D+/) || [''])[0], number = (value.match(/\d+/) || ['0'])[0]; return string + (+number + 1).toString().padStart(number.length, 0); } function assertEquals(a, b) { console.log(a === b, a, b); } assertEquals(incrementString("foobar000"), "foobar001"); assertEquals(incrementString("foo"), "foo1"); assertEquals(incrementString("foobar001"), "foobar002"); assertEquals(incrementString("foobar99"), "foobar100"); assertEquals(incrementString("foobar099"), "foobar100"); assertEquals(incrementString(""), "1");

暂无
暂无

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

相关问题 为什么即使我将它转换为数组,我的 querySelectorAll 也不起作用? - Why is my querySelectorAll not working even though I converted it to an array? 为什么我必须将我的字符串推入数组中才能再次将其重新加入? - Why do I have to push my string into an array only to join it back again? 即使我在后台运行了模拟器,也无法运行我的React本机代码 - I can't run my react native code even though I have an emulator running in the back round 如何将数字推入数组? - How can I push a number into an array? 检查数组中可以成功转换为数字的字符串数 - Checking number of strings inside array that can be successfully converted into a number 返回数组中可以成功转换为数字的字符串数量的计数 - Return a count of the number of strings in the array that can be successfully converted into a number 即使我有一个数组,map 也不是 function - map is not a function even though I have an array 为什么我的 chrome 开发工具内存选项卡中有大量相同 html 的“连接字符串” - Why do I have a huge number of 'concatenated strings' of the same html in my chrome dev tools memory tab 为什么 push 显示为未定义,即使它是一个关键字并且我没有将其声明为任何 object? - Why is push shown as undefined, even though it is a keyword and i have not declared this as any object? 即使我的代码看起来很好,我仍然会收到“未捕获的TypeError:字符串不是函数”错误。任何人都可以看看并解释一下吗? - I keep getting a “Uncaught TypeError: string is not a function” error, even though my code seems to be fine. Can anyone have a look and explain?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM