我从fetch中遇到过这种奇怪的行为。
首先看一下长度(正确)和实际结构的截图:@orlp created the GOLF architecture with a simulator that prints timing results
考虑来自服务器的以下json响应,其中likes
数组明确包含一个对象,comments
数组包含两个:
{
"status":"success",
"payload":{
"272699880986":{
"likes":[
{
"createdTime":"2016-11-07T04:41:21.000Z",
"senderId":10209615042475034,
"senderName":"Alfredo Re",
"likes":1
}
],
"comments":[
{
"createdTime":"2016-11-07T04:41:54.000Z",
"senderId":1021426764639564,
"senderName":"Alfredo J. Re",
"comments":1
},
{
"createdTime":"2016-11-07T04:41:24.000Z",
"senderId":10209615042475034,
"senderName":"Alfredo Re",
"comments":1
}
]
}
}
}
现在,将响应传递给整个response.json()
,如下所示:
fetch('http://example.com/entries.json', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
...authKeys,
}
})
.then(response => {
switch(response.status){
case 200:
console.log('status 200', response)
return response.json()
}
})
.then(json => {
console.log('parsed response')
console.log(json)
})
获取以下结果:
{
"status":"success",
"payload":{
"272699880986":{
"likes":[
{
"createdTime":"2016-11-07T04:41:54.000Z",
"senderId":1021426764639564,
"senderName":"Alfredo J. Re",
"likes": 1,
"comments":1
},
{
"createdTime":"2016-11-07T04:41:24.000Z",
"senderId":10209615042475034,
"senderName":"Alfredo Re",
"comments":1
}
],
"comments":[
{
"createdTime":"2016-11-07T04:41:54.000Z",
"senderId":1021426764639564,
"senderName":"Alfredo J. Re",
"comments":1
},
{
"createdTime":"2016-11-07T04:41:24.000Z",
"senderId":10209615042475034,
"senderName":"Alfredo Re",
"comments":1
}
]
}
}
}
了解它如何混淆likes
和comments
个集合。这让我现在疯了。我错过了什么吗?
更新
这是console screenshot列出显示和包含一个对象的数组,另一个包含两个对象。当我点击前者时,它会变成一个有两个对象的数组!这令我感到困惑
答案 0 :(得分:0)
好的,我发现它实际上是lodash的_.merge()
在取出它之后改变了我的物体。我正在使用lodash根据senderId
键合并两个集合(喜欢和评论)。
为了记录,我最终使用了这种方法:https://stackoverflow.com/a/35094948/1418038