【发布时间】:2018-03-18 17:54:56
【问题描述】:
以下代码 examines the content of inputData.body for a 32-character string match,并且 - 据我所知 - 将所有匹配项放在一个数组中。
// stores an array of any length (0 or more) with the matches
var matches = inputData.body.match(/\b[\w-]{32}\b/g)
// the .map function executes the nameless inner function once for each element of the array and returns a new array with the results
return matches.map(function (m) { return {str: m} })
我现在需要代码在没有匹配表达式的情况下返回 something,例如。字符串"false"。
我无法让这个补充工作......
// stores an array of any length (0 or more) with the matches
var matches = inputData.body.match(/\b[\w-]{32}\b/g)
if (matches == null){
return 'false'
}
// the .map function executes the nameless inner function once for each element of the array and returns a new array with the results
return matches.map(function (m) { return {str: m} })
如果出现空虚,我应该如何有条件地返回一些东西?
【问题讨论】:
-
代码是否封装在函数中?
-
我认为你的代码应该可以工作。
-
你确定调用者准备好获取一个字符串作为函数的结果吗?它可能需要一个数组。
-
也许你应该在没有匹配的时候返回一个空数组。
-
你的意思是你不能让它工作?它做了什么而不是返回字符串
'false'?
标签: javascript arrays is-empty