我遇到了一个相当奇怪的错误,我觉得我对Object.values
和Object.entries
的预期行为的理解可能不完整。我有一个包含五个字符串的对象,我从文件导入。记录时,我可以看到对象是正确导入的,所有五个字符串都在那里。当我使用Object.values
或Object.entries
时,生成的数组只包含四个元素,最后一个元素被跳过。但是,当与解构一起使用时,一切都按预期工作,返回所有五个元素。你能看看下面的代码,让我知道我可能会缺少什么吗?您能否在此结束时重现这一点?
// reducers.js
const Auth = 'Auth';
const ErrorManager = 'ErrorManager';
const Loading = 'Loading';
const Posts = 'Posts';
const UserData = 'UserData';
export { Auth, ErrorManager, Loading, Posts, UserData };
// another file
import * as reducers from './reducers';
console.log('This is the object:')
console.log(JSON.stringify(reducers));
console.log("This array is incomplete:");
console.log(JSON.stringify(Object.values(reducers)));
console.log("This array is complete:");
console.log(JSON.stringify(Object.values({ ...reducers })));
日志输出如下:
This is the object:
{"Auth":"Auth","ErrorManager":"ErrorManager","Loading":"Loading","Posts":"Posts","UserData":"UserData"}
This array is incomplete:
["Auth","ErrorManager","Loading","Posts"]
This array is complete:
["Auth","ErrorManager","Loading","Posts","UserData"]
如果我不使用导入,一切都按预期工作,请参阅下文:
const Auth = 'Auth';
const ErrorManager = 'ErrorManager';
const Loading = 'Loading';
const Posts = 'Posts';
const UserData = 'UserData';
const reducers = { Auth, ErrorManager, Loading, Posts, UserData };
console.log('This is the object:')
console.log(JSON.stringify(reducers));
console.log("This array is complete:");
console.log(JSON.stringify(Object.values(reducers)));
日志输出:
This is the object:
{"Auth":"Auth","ErrorManager":"ErrorManager","Loading":"Loading","Posts":"Posts","UserData":"UserData"}
This array is complete:
["Auth","ErrorManager","Loading","Posts","UserData"]