[英]Chrome console shows Uncaught TypeError: Cannot read property 'start' of undefined
[英]Chrome console shows object property as undefined
我是对象编程的新手。 我正在将对象记录到Chrome控制台,并且无法注意到奇怪的行为。
一些属性在日志的底部显示为undefined,但在顶部具有值。
当我从函数中返回对象时,它会返回已定义的相同属性,因此会定义Dirty Sock
和Microphone
。 那么,为什么控制台认为它们不是?
我的代码:
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;
}, {});
}
}
arr1
和arr2
值如下:
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"]
];
console.log输出(在Chrome控制台中)末尾的信息框可能会指出该输出刚刚被重新计算。 如果您想在输出时了解对象的状态,建议先创建一个字符串,然后再输出。 在您的示例中,尝试:
console.log(JSON.stringify(currentInv));
然后看看会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.