所以我有这个对象,当我console.log它并在检查器中查看它时,它看起来很好。 Object in Inspector
但是当我打印出对象的键时,我只得到以下结果: Object Keys in Inspector
之后我打印出的所有内容(object.value)看起来像这样:
f (){return f&&(c&&!b&&(h=f.length-1,g.push(c)),function d(b){r.each(b,function(b,c) ){r.isFunction(c)?a.unique&&j.has(c)||f.push(c):c&&c.length&&"string"!==r.type(c)&&d(c)})}(参数),c&&!b&&i()),这个}
如何仅访问第一个屏幕截图中的“responseJSON”?
一些可能很重要的背景信息: 我从 Spotify Web-API 获取了对象,授权等工作一切正常。
function getUserData(accessToken) {
return $.ajax({
url: 'https://api.spotify.com/v1/me/player/currently-playing',
headers: {
'Authorization': 'Bearer ' + accessToken
}
});
}
最佳答案
这是因为您正在枚举 jQuery 延迟对象属性。延迟对象是异步的,在您的例子中来自 HTTP 请求。
要正确枚举对象,您应该首先使用 Deferred.then
等待它可用。替换您的代码:
console.log(Object.keys(your_object))
至:
your_object.then(function(result) {
console.log(Object.keys(result))
}).catch(function(err) {
console.error(err)
})
关于JavaScript 对象访问;输出正常,选择不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47491041/