Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

javascript - 输出结果时获取[对象对象](Getting [object Object] when outputting result)

I am not sure why I am getting [object Object] .(我不确定为什么会得到[object Object] 。)

I want to be able to work with the data that comes out of it.(我希望能够处理其中的数据。) If you have any ideas I would appreciate it.(如果您有任何想法,我将不胜感激。) Thank you!(谢谢!) const result = async () => { const response = await fetch( 'https://cloud.iexapis.com/v1/stock/MSFT/chart?token=pk_0f5e9e6bca7548918adc6512bcb57ff4' ); const myJson = await response.json(); return async () => { for await (const item of myJson) { return { date: Date.parse(item.date), open: item.open, high: item.high, low: item.low, close: item.close, volume: item.volume, }; } }; }; result().then( item => console.log(JSON.stringify(`result data: ${item}`)) // console.log(`result data: ${item}`) ); edited:(编辑:) result().then(item => console.log('result data:', item));   ask by John John translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use JSON.stringify() for the object only.(您只需要对对象使用JSON.stringify() 。)

Currently, your object is concatenating with the string value which will make the entire result as string and stringifying it will then show the object as string.(当前,您的对象正在与字符串值连接,该值将使整个结果成为字符串,然后对其进行字符串化,然后将该对象显示为字符串。) const result = () => { return { someProperty: 12 }; }; var res = result(); console.log('result data:' + JSON.stringify(res)) Even if you do not use JSON.stringify() the way you did, it implicitly convert the object to string as shown below.(即使您不像JSON.stringify()那样使用JSON.stringify() ,它JSON.stringify()隐式将对象转换为字符串,如下所示。) That is why your code do not work:(这就是为什么您的代码不起作用的原因:) const result = () => { return { someProperty: 12 }; }; var res = result(); console.log(`result data: (${res})`);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...