我正在研究商店定位器,需要创建一个对象数组(称为开业者数据),每个对象都是一个商店。可以,但是我推入数组的任何对象都不起作用。
代码如下:
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]的值和类型未在控制台中显示,并且未定义。 如果查看整个数组,则会看到每个索引处的对象。
为什么当推入一个对象时它会保留一个对象,但是如果我推入一个代表对象的变量却变得不确定?
答案 0 :(得分:1)
您仅将一个对象推入数组。尝试在使用console.log()之前添加practitionersData.push(temperedOBJ)
答案 1 :(得分:1)
==== start practitionersData =====
[
{
"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"
},
{
"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"
}
]
object
==== start practitionersData 0 =====
{
"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"
}
object
==== start practitionersData 1 =====
{
"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"
}
object
请查看执行代码后得到的输出。我得到了“对象”作为输出并且没有变得不确定。