我正在商店定位器上工作,需要创建一个对象数组(称为开业者数据),每个对象都是一个商店。它可以工作,但是我推入数组的任何对象都不起作用。

代码如下:



let practitionersData = [];
practitionersData.push({
    "name": "Chipotle Minneapolis",
    "address": "3045 Excelsior Blvd",
    "address2": "",
    "city": "Minneapolis",
    "state": "MN",
    "postal": "55416",
    "phone": "612-922-6662",
    "web": "http://www.chipotle.com",
    "hours1": "Mon-Sun 11am-10pm",
    "hours2": "",
    "hours3": "",
    "featured": "",
    "features": "",
    "lat": "44.947464",
    "lng": "-93.320826",
    "date": "10/17/18",
    "formattedaddress":"3040 Excelsior Blvd, Minneapolis MN 55416"
    });

let temperedOBJ = {
        "name": "Chipotle Minneapolis2",
        "address": "30451 Excelsior Blvd",
        "address2": "",
        "city": "Minneapolis",
        "state": "MN",
        "postal": "55416",
        "phone": "612-922-6662",
        "web": "http://www.chipotle.com",
        "hours1": "Mon-Sun 11am-10pm",
        "hours2": "",
        "hours3": "",
        "featured": "",
        "features": "",
        "lat": "44.947464",
        "lng": "-93.320826",
        "date": "10/17/18",
        "formattedaddress":"3040 Excelsior Blvd, Minneapolis MN 55416"
}

practitionersData.push(temperedOBJ);

console.log('==== start practitionersData =====')
console.log(practitionersData);
console.log(typeof practitionersData);
console.log('==== start practitionersData 0 =====')
console.log(practitionersData[0]);
console.log(typeof practitionersData[0]);
console.log('==== start practitionersData 1 =====')
console.log(practitionersData[1]);
console.log(typeof practitionersData[1]); 





实践者数据[0]的值和类型看起来不错并显示一个对象。
实践者数据[1]的值和类型未在控制台中显示,并且未定义。
如果查看整个数组,则会看到每个索引处的对象。

为什么当推入一个对象时它会保留一个对象,但是如果我推入一个代表对象的变量,它为什么变得不确定?

最佳答案

您仅将一个对象推入阵列。在使用console.log()之前尝试添加practitionersData.push(temperedOBJ)

关于javascript - 将对象插入数组会产生未定义的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52205373/

10-11 23:24
查看更多