简体   繁体   English

尝试访问 object 中的值时,javascript 变量未定义错误?

[英]javascript variable is not defined error when try to access the value inside the object?

  const roomAndReservationBinding = (rooms, reservations) => {
    var roomInfo = rooms;
    for (var i = 0; i <= roomInfo.length; i++) {
      var room = roomInfo[i];

      console.log(room.id);
    }
  };

// on console i get the IDs printed as expected, but on the browser i get the following error, room is undefined on the line of console.log() function // 在控制台上,我按预期打印了 ID,但在浏览器上,我收到以下错误,console.log() function 行中的房间未定义

This is the root cause -:这是根本原因-:

for (var i = 0; i <= roomInfo.length; i++) {

You are iterating one extra time, it should be this -:你正在迭代一次额外的时间,它应该是这样的 - :

for (var i = 0; i < roomInfo.length; i++) {

 const roomAndReservationBinding = (rooms, reservations) => { var roomInfo = rooms; for (var i = 0; i < roomInfo.length; i++) { var room = roomInfo[i]; console.log(room.id); } }; roomAndReservationBinding([{id:1},{id:2},{id:3}],[])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM