简体   繁体   English

Chrome控制台将对象属性显示为未定义

[英]Chrome console shows object property as undefined

I've a newbie on object programming. 我是对象编程的新手。 I was logging an object to the Chrome console and couldn't fail to notice a weird behavior. 我正在将对象记录到Chrome控制台,并且无法注意到奇怪的行为。

一些对象属性显示为未定义,并且好像它们同时具有值

Some properties appear as undefined at the bottom of the log, but have value on top. 一些属性在日志的底部显示为undefined,但在顶部具有值。

When I return the object from my function it returns with those same properties defined, therefore Dirty Sock and Microphone are defined. 当我从函数中返回对象时,它会返回已定义的相同属性,因此会定义Dirty SockMicrophone So why does the console think they are not? 那么,为什么控制台认为它们不是?

My code: 我的代码:

function updateInventory(arr1, arr2) {
  let currentInv = array2DToObject(arr1);
  let newItems = array2DToObject(arr2);
  console.log(currentInv); //This is the log I'm speaking about.

  for(let key in currentInv) {
    if(newItems.hasOwnProperty(key)) {
      currentInv[key] = currentInv[key] + newItems[key];
      delete newItems[key];
    } else {
      currentInv[key] = newItems[key];
      delete newItems[key];
    }  
  }

  if(Object.keys(newItems === 0) && newItems.constructor === Object) {
    return currentInv;
  } else {
    return 'A mistake has occured, newItems obj has not been emptied correctly';
  }

  function array2DToObject (arr) {
     return arr.reduce((acc, curr) => {
      acc[curr[1]] =  curr[0] ;
      return acc;
    }, {});
  }
}

Values for arr1 and arr2 are as follow: arr1arr2值如下:

   arr1 = [
    [21, "Bowling Ball"],
    [2, "Dirty Sock"],
    [1, "Hair Pin"],
    [5, "Microphone"]
];

 arr2 = [
    [2, "Hair Pin"],
    [3, "Half-Eaten Apple"],
    [67, "Bowling Ball"],
    [7, "Toothpaste"]
];

The information box at the end of your console.log output (in the Chrome console) is likely to state that the output has just been recalculated. console.log输出(在Chrome控制台中)末尾的信息框可能会指出该输出刚刚被重新计算。 If you want to know the state of your objects at the time of outputting, I would suggest creating a string first and then outputting. 如果您想在输出时了解对象的状态,建议先创建一个字符串,然后再输出。 In your example, try: 在您的示例中,尝试:

console.log(JSON.stringify(currentInv));

And then see what happens. 然后看看会发生什么。

暂无
暂无

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

相关问题 Chrome控制台显示未捕获的TypeError:无法读取未定义的属性“开始” - Chrome console shows Uncaught TypeError: Cannot read property 'start' of undefined 尽管Chrome控制台树视图中存在对象属性(类型=对象),但未定义 - Object property (type = object) is undefined despite existing in chrome console tree view 为什么我console.log一个对象,它显示对象,但是当我console.log时,它显示未定义 - Why I console.log a Object,it shows object,but when I console Object.value ,it shows undefined 记录到Chrome控制台的对象的属性不存在 - Object logged to Chrome console has a nonexistent property 记录到控制台会显示已定义的值,但将属性错误视为未定义 - Logging to console shows defined value, but using property errors out as undefined 对象显示在控制台中,但始终在启动时添加未定义或空数组 - Object shows up in console but always adds an undefined or empty array at start OOO javascript:object 自参考返回未定义但控制台显示它存在 - OOO javascript: object self reference returns undefined but console shows it exists 控制台中的对象显示带有值的getter,但在代码中返回未定义 - Object in console shows getter with a value but in the code it returns undefined JS 说 object 是未定义的,即使它显示在 console.log 中 - JS says an object is undefined even though it shows with console.log Chrome控制台Javascript“无法读取未定义的属性'click'” - Chrome Console Javascript “Cannot read property 'click' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM