我遇到了错误:
未捕获的TypeError:无法读取第5行上未定义的属性'length'
当我将其输入控制台时。我不确定为什么将senArr[i]
拆分为数组然后传递给for循环时说undefined
是function LongestWord(sen) {
var senArr = sen.split(' ');
var longestWord = senArr[0];
for (let i = 1; i <= senArr.length; i++) {
if (senArr[i].length > longestWord.length) {
longestWord = senArr[i];
}
} return longestWord;
}
。
我一直在试图找出为什么出现此错误...
-for循环有权访问父作用域
-senArr分为一个数组,我认为使用每个索引中存储的字符串的长度没有任何问题。
出于想法,我开始认为问题出在代码中的其他地方。
public function requestPassAction(Request $request, TokenGeneratorInterface $tokenGenerator = null)
{
$email = $request->request->get('_username');
$form = $this->createFormBuilder()
->add('username', EmailType::class, [
'constraints' => [new Email(), new NotBlank()]
])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository(User::class)
->findOneBy(array('username' => $email));
if (!$user) {
return $this->redirectToRoute("renouvellement_pass");
}
$user->setToken($tokenGenerator->generateToken());
$user->setPasswordRequestedAt(new \Datetime());
$em->flush();
return $this->redirectToRoute("login");
}
return $this->render('Resetting/request.html.twig', [
'form' => $form->createView()
]);
答案 0 :(得分:2)
啊,将您的<=
更改为<
,因为数组中的最后一个索引是senArr.length - 1
。
function LongestWord(sen) {
var senArr = sen.split(' ');
var longestWord = senArr[0];
for (let i = 1; i < senArr.length; i++) {
console.log('i is ', i, ' and word is ', senArr[i]);
if (senArr[i].length > longestWord.length) {
longestWord = senArr[i];
}
}
return longestWord;
}
console.log(LongestWord('hi there this is a test'));
使用<=
,您将超越最后一个索引(在上例中,i
变为6),并且该索引处没有字符串,这就是您看到错误的原因。
答案 1 :(得分:1)
问题是,当您与<=进行比较时,要包含数组length + 1,请注意数组是从零开始的索引,因此最后一项是length-1
答案 2 :(得分:0)
你在这里...
var LongestWord = (sen) => {
var senArr = sen.split(' ');
console.log(senArr);
var longestWord = '';
for (let i = 0; i <= senArr.length - 1; i++) {
if (senArr[i].length > longestWord.length) {
longestWord = senArr[i];
}
} return longestWord;
}
console.log(LongestWord('alongword is the longest'));
答案 3 :(得分:0)
我刚刚将其转换为现代javascript,它的工作原理是:-)
public class AssemblyTextFileReader
{
private readonly Assembly _assembly;
public AssemblyTextFileReader(Assembly assembly)
{
_assembly = assembly ?? throw new ArgumentNullException(nameof(assembly));
}
public async Task<string> ReadFileAsync(string fileName)
{
var resourceName = _assembly.GetManifestResourceName(fileName);
using (var stream = _assembly.GetManifestResourceStream(resourceName))
{
using (var reader = new StreamReader(stream))
{
return await reader.ReadToEndAsync();
}
}
}
}
public static class AssemblyExtensions
{
public static string GetManifestResourceName(this Assembly assembly, string fileName)
{
string name = assembly.GetManifestResourceNames().SingleOrDefault(n => n.EndsWith(fileName, StringComparison.InvariantCultureIgnoreCase));
if (string.IsNullOrEmpty(name))
{
throw new FileNotFoundException($"Embedded file '{fileName}' could not be found in assembly '{assembly.FullName}'.", fileName);
}
return name;
}
}