Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中的TypeError,forEach为未定义_Javascript_Html_Loops_Object_Foreach - Fatal编程技术网

JavaScript中的TypeError,forEach为未定义

JavaScript中的TypeError,forEach为未定义,javascript,html,loops,object,foreach,Javascript,Html,Loops,Object,Foreach,我正在尝试用我的代码修复一个错误-该问题是由错误消息中的类型错误引起的: test.js:89 Uncaught TypeError: Cannot read property 'forEach' of undefined at printResults (test.js:89) at test.js:105 代码如下: const phones = [{ name: "iPhone XS", brand: "Apple", cost: 43, data: "500

我正在尝试用我的代码修复一个错误-该问题是由错误消息中的类型错误引起的:

test.js:89 Uncaught TypeError: Cannot read property 'forEach' of undefined
    at printResults (test.js:89)
    at test.js:105

代码如下:


const phones = [{
    name: "iPhone XS", brand: "Apple", cost: 43, data: "500MB", minutes: "Unlimited", texts: "Unlimited"
},
    {
        name: "iPhone 11", brand: "Apple", cost: 64, data: "90GB", minutes: "Unlimited", texts: "Unlimited"
    },
    {
        name: "Galaxy S10", brand: "Samsung", cost: 30, data: "20GB", minutes: "Unlimited", texts: "Unlimited"
    },
    {
        name: "Galaxy S10", brand: "Samsung", cost: 65, data: "Unlimited", minutes: "Unlimited", texts: "Unlimited"
    },
    {
        name: "Galaxy A10", brand: "Samsung", cost: 11.99, data: "500MB", minutes: 250, texts: "Unlimited"
    },
    {
        name: "Galaxy S9", brand: "Samsung", cost: 31, data: "20GB", minutes: "Unlimited", texts: "Unlimited"
    },
    {
        name: "StarTAC 130", brand: "Motorola", cost: 3, data: "0MB", minutes: 200, texts: 500
    },
    {
        name: "Pixel 3A", brand: "Google", cost: 23, data: "4GB", minutes: "Unlimited", texts: "Unlimited"
    },
    {
        name: "Xperia 10", brand: "Sony", cost: 30, data: "20GB", minutes: "Unlimited", texts: "Unlimited"
    },
    {
        name: "P30", brand: "Huawei", cost: 27.99, data: "500MB", minutes: 500, texts: "Unlimited"
    }];






// Functions

function getUserPreferences() {

    // These are asking the user for entry of the data into the system.
        const userPrompt = [
        {
            phoneBrand: prompt("Enter a brand name") 
        },

        {
            phoneCost: prompt("Enter a monthly cost") 
        },

        {
            phoneData: prompt("Enter the amount of data") 
        },

        {
            phoneMins: prompt("How many minutes?") 
        },

        {
            phoneTexts: prompt("How many texts?") 
        },



        ]

}

function getMatchingPlans(phoneBrand, phoneCost, phoneData, phoneMins, phoneTexts) {

    // This is then filtering the object of phones to match what the user has entered into the system.

    const matchingPhones = phones.filter(function(phone) {
        if(phone.brand===phoneBrand && phone.cost.toString()<=phoneCost && phone.data<=phoneData && phone.minutes.toString()<=phoneMins && phone.texts.toString()<=phoneTexts) {
                return true;
    }
     return false;

})

}

function printResults() {

    // This is then displaying data in the system.

        const returnPhones = document.querySelector("#returnPhones");

        matchingPlans.forEach(function(phone) {

        const newList = document.createElement("ul");
        newList.textContent=phone.name;
        returnPhones.appendChild(newList);

})


}




const userPrefs = getUserPreferences();
const matchingPlans = getMatchingPlans(userPrefs);
printResults(matchingPlans);


常数电话=[{
名称:“iPhone XS”,品牌:“苹果”,成本:43,数据:“500MB”,分钟:“无限”,文字:“无限”
},
{
名称:“iPhone 11”,品牌:“苹果”,成本:64,数据:“90GB”,分钟:“无限”,文字:“无限”
},
{
名称:“Galaxy S10”,品牌:“三星”,成本:30,数据:“20GB”,分钟:“无限”,文字:“无限”
},
{
名称:“Galaxy S10”,品牌:“三星”,成本:65,数据:“无限”,分钟:“无限”,文字:“无限”
},
{
名称:“Galaxy A10”,品牌:“三星”,成本:11.99,数据:“500MB”,分钟:250,文字:“无限”
},
{
名称:“Galaxy S9”,品牌:“三星”,成本:31,数据:“20GB”,分钟:“无限”,文字:“无限”
},
{
名称:“StarTAC 130”,品牌:“Motorola”,成本:3,数据:“0MB”,分钟:200,文本:500
},
{
名称:“像素3A”,品牌:“谷歌”,成本:23,数据:“4GB”,分钟:“无限”,文字:“无限”
},
{
名称:“Xperia 10”,品牌:“索尼”,成本:30,数据:“20GB”,分钟:“无限”,文字:“无限”
},
{
名称:“P30”,品牌:“华为”,成本:27.99,数据:“500MB”,分钟:500,文字:“无限”
}];
//功能
函数getUserPreferences(){
//它们要求用户将数据输入系统。
const userPrompt=[
{
phoneBrand:提示(“输入品牌名称”)
},
{
电话成本:提示(“输入每月成本”)
},
{
phoneData:提示(“输入数据量”)
},
{
phoneMins:prompt(“几分钟?”)
},
{
PhoneText:提示(“有多少条短信?”)
},
]
}
函数getMatchingPlans(phoneBrand、phoneCost、phoneData、phoneMins、PhoneText){
//然后过滤手机对象,以匹配用户输入系统的内容。
const matchingPhones=phones.filter(函数(电话){

如果(phone.brand===phoneBrand&&phone.cost.toString()错误与脚本类型无关,则您正在使用未定义的值调用forEach。因此,根据您的代码,在
printResults
函数中未定义匹配计划

另一个问题是您正在调用
printfresults(matchingplan);
但是您的
printfresults
不接受任何参数


另一个问题是,您似乎期望
getMatchingPlans
返回一个数组,但您返回的是布尔值

另一个问题是没有使用箭头功能,所以

const matchingPhones = phones.filter(function(phone) {
变成

const matchingPhones = phones.filter((phone) => {

其他地方

getMatchingPlans
从不返回任何内容,因此调用它的结果是
undefined
。您也没有将
matchingPlans
声明为
printResults
中的参数,因此
printResults
从外部范围使用
matchingPlans
,这不是最佳做法。您是pas将其作为参数使用,因此声明一个参数来接收它:
函数printResults(matchingPlans){