简体   繁体   English

对不可迭代实例的解构尝试无效。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法

[英]Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method

i'm using react native with regex我正在使用带有正则表达式的本机反应

if i use my code如果我使用我的代码

this error occured发生此错误

Invalid attempt to destructure non-iterable instance.对不可迭代实例的解构尝试无效。 In order to be iterable, non-array objects must have a Symbol.iterator method.为了可迭代,非数组对象必须具有 Symbol.iterator 方法。

I want to put an empty value like ' ' in the match if any character other than a number and decimal point is included in my regular expression.如果我的正则表达式中包含除数字和小数点以外的任何字符,我想在匹配项中放置一个空值,例如 ' '。 If you put "123abc" in the value variable, match returns "123", but if you put "acv" in the value constant, the above error occurs.如果将“123abc”放入 value 变量中,则 match 返回“123”,但如果将“acv”放入 value 常量中,则会出现上述错误。 In this case, how can I put an empty string into the match without generating an error?在这种情况下,如何将空字符串放入匹配项而不产生错误?

 const regex = /\d+(\.\d{1,2})?/;

const value = "abd"
const [match] = regex.exec(value);

you can use ||你可以使用|| operator.操作员。 in case exec return null it will default to empty string '' .如果exec返回null ,它将默认为空字符串''

 function getStrings(value) { const regex = /\d+(\.\d{1,2})?/; const [match] = [regex.exec(value) || '']; if(Array.isArray(match)) return match[0] return match } console.log(getStrings("abd")) console.log(getStrings("123abd"))

暂无
暂无

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

相关问题 TypeError:无效的解构不可迭代实例的尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() - TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() TypeError:解构不可迭代实例的无效尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法 - TypeError: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method 获取错误为“传播不可迭代实例的尝试无效。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法' - Getting error as 'Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method' React js:传播不可迭代实例的无效尝试。 为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法 - React js : Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method 解构不可迭代实例的无效尝试 - Invalid attempt to destructure non-iterable instance 未处理的拒绝(TypeError):破坏非迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to destructure non-iterable instance 未捕获的 TypeError:无效的解构不可迭代实例的尝试 - Uncaught TypeError: Invalid attempt to destructure non-iterable instance TypeError:对不可迭代实例 React/Jest 的解构尝试无效 - TypeError: Invalid attempt to destructure non-iterable instance React/Jest 解构不可迭代实例的无效尝试 - React Native - Invalid attempt to destructure non-iterable instance - React Native 将项目添加到 Reducer 中的空数组中会给出错误“类型错误:传播不可迭代实例的尝试无效”。 - Add Items into Empty array in Reducer give Error 'TypeError: Invalid attempt to spread non-iterable instance.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM