Javascript TypeError:无法读取属性';长度';未定义的反应

Javascript TypeError:无法读取属性';长度';未定义的反应,javascript,reactjs,undefined,create-react-app,Javascript,Reactjs,Undefined,Create React App,我正在做一个简单的反应练习。该练习的目标只是: 从数组中随机抽取水果 记录信息“我想要一个水果,谢谢。” 记录信息“给你:随机水果” 记录信息“美味!我可以再来一杯吗?” 从水果阵列中移除水果 记录消息“对不起,我们都出去了,我们还有剩余的水果。” 在运行此代码时,我遇到了以下错误。出于某种原因,“长度”似乎是问题所在 TypeError: Cannot read property 'length' of undefined Module../src/index.js C:/Users/ja

我正在做一个简单的反应练习。该练习的目标只是:

  • 从数组中随机抽取水果
  • 记录信息“我想要一个水果,谢谢。”
  • 记录信息“给你:随机水果”
  • 记录信息“美味!我可以再来一杯吗?”
  • 从水果阵列中移除水果
  • 记录消息“对不起,我们都出去了,我们还有剩余的水果。”
在运行此代码时,我遇到了以下错误。出于某种原因,“长度”似乎是问题所在

TypeError: Cannot read property 'length' of undefined
Module../src/index.js
C:/Users/jaina/Desktop/ReactAPPS/exercise1/exercise-one/src/index.js:15
  12 | // Remove the fruit from the array of fruits
  13 | let remaining = remove(foods, fruit);
  14 | // Log the message “I’m sorry, we’re all out. We have FRUITSLEFT left.”
> 15 | console.log(`I’m sorry, we’re all out. We have ${remaining.length} other foods left.`);
  16 | 
它还说
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

index.js

import foods from './foods';
import { choice, remove } from './helpers';

// Randomly draw a fruit from the array
let fruit = choice(foods);
// Log the message “I’d like one RANDOMFRUIT, please.”
console.log(`I’d like one ${fruit}, please.`);
// Log the message “Here you go: RANDOMFRUIT”
console.log(`Here you go: ${fruit}`);
// Log the message “Delicious! May I have another?”
console.log('Delicious! May I have another?');
// Remove the fruit from the array of fruits
let remaining = remove(foods, fruit);
// Log the message “I’m sorry, we’re all out. We have FRUITSLEFT left.”
console.log(`I’m sorry, we’re all out. We have ${remaining.length} other foods left.`);
const foods = [
    "In 
helper.js
, if your function
remove
does not find anything, it will return undefined. Then, when you say...

console.log(`I’m sorry, we’re all out. We have ${remaining.length} other foods left.`);
Foods.js

import foods from './foods';
import { choice, remove } from './helpers';

// Randomly draw a fruit from the array
let fruit = choice(foods);
// Log the message “I’d like one RANDOMFRUIT, please.”
console.log(`I’d like one ${fruit}, please.`);
// Log the message “Here you go: RANDOMFRUIT”
console.log(`Here you go: ${fruit}`);
// Log the message “Delicious! May I have another?”
console.log('Delicious! May I have another?');
// Remove the fruit from the array of fruits
let remaining = remove(foods, fruit);
// Log the message “I’m sorry, we’re all out. We have FRUITSLEFT left.”
console.log(`I’m sorry, we’re all out. We have ${remaining.length} other foods left.`);
const foods = [
    "In 
helper.js
, if your function
remove
does not find anything, it will return undefined. Then, when you say...

console.log(`I’m sorry, we’re all out. We have ${remaining.length} other foods left.`);
const foods=[

“在
helper.js
中,如果您的函数
remove
没有找到任何内容,它将返回undefined。然后,当您说

function remove(items, item){
    for (let i = 0; i < items.length; i++){
        if(items[i] === item){
            return [ ...items.slice(0,i), ...items.slice(i+1)];
        }
    }
    // change this to whatever you want in case it was able to find item to remove
    return items;
}
…您假设剩余的
是一个数组,但它实际上是未定义的


尝试将
return items;
放在
remove
函数的末尾,在for循环之后。

在上面的代码中,如果
remove
函数中找不到任何内容,则返回
items

function choice(items){
    let idx = Math.floor(Math.random()* items.length);
}
功能删除(项目,项目){
for(设i=0;i
您的问题似乎在这里:

function choice(items){
   let fruitIndex = Math.floor(Math.random()* items.length);
   return items[fruitIndex];
}
所以
让水果=选择(食物)
,水果永远是未定义的

尝试按如下方式返回helper函数的值:


您还应该找到选项的索引并返回水果,否则
选项将只返回一个数字。

修改您的选项并删除函数

功能选择(项目){
返回Math.floor(Math.random()*items.length);
}
功能移除(项目,项目){
返回项目。过滤器(e=>e!==item);
};

我猜,
如果
条件不匹配,它就不会进入块内部,也不会运行
返回
。因此,它只是隐式返回
未定义的
。还要验证您的数据
${remaining&&remaining.length}
非常感谢。我添加了它,程序运行成功。谢谢。