node.js - javascrpit 中的 Array.push 用最后一个推送的值替换所有预先存在的值,保持长度不变
问题描述
我正在抓取网站的数据,并在将对象推送到 javascript 列表时生成最终结果,它将所有预先存在的值替换为最后推送的值。
当我在推送之前 console.logged 它时,我得到了正确的答案。
function getCharactersfor(name,filter='anime'){
return rp("https://www.anime-planet.com/"+filter+"/"+change(name)+"/characters").then((data) => {
const $=cheerio.load(data)
result=[]
a=$("tr").each(function(){
character={name:undefined,image:undefined,voiceActors:{English:[],Japanese:[]}}
character.name=$(this).find(".name").text()
character.image="https://www.anime-planet.com"+$(this).find('.tableAvatar').find('img').attr('src')
if(filter=='anime'){
res={name:'',image:undefined,otherworks:[]}
m=$(this).find(".flagUS").children().each(function(i){
res.name=$(this).html()
a=cheerio.load($(this).attr('title'))
res.image="https://www.anime-planet.com"+a('img').attr('src')
character.voiceActors.English.push(res)
})
m=$(this).find(".flagJP").children().each(function(){
res.name=$(this).html()
a=cheerio.load($(this).attr('title'))
res.image="https://www.anime-planet.com"+a('img').attr('src')
character.voiceActors.Japanese.push(res)
})
}else{
character.voiceActors=undefined
}
result.push(character)
})
return(result)
}).catch(err=> console.log(err))
}
getCharactersfor('black clover').then((result)=>{
console.log(result[0].voiceActors)
})
结果是
{ English:
[ { name: 'Gakuto KAJIWARA',
image: 'https://www.anime-planet.com/images/people/thumbs/gakuto-kajiwara-23630.jpg?t=1509881610' } ],
Japanese:
[ { name: 'Gakuto KAJIWARA',
image: 'https://www.anime-planet.com/images/people/thumbs/gakuto-kajiwara-23630.jpg?t=1509881610' },
{ name: 'Gakuto KAJIWARA',
image: 'https://www.anime-planet.com/images/people/thumbs/gakuto-kajiwara-23630.jpg?t=1509881610' } ] }
他们都应该是不同的
解决方案
推荐阅读
- git - 在 Azure 中创建 Git 分支,如何在 Visual Studio 中连接或签出
- python-3.x - 如何通过用户输入打开文件?
- typo3 - TYPO3 8 及更高版本中 pageOverlayFields-Feature 的替换
- c# - webapi 的基于角色的身份验证
- python-3.x - 使用服务器端加密的 S3 上传(python SDK)
- android - 在 Oppo 手机上以编程方式发送短信
- visual-studio-code - 如何为 VSCode 在 zen 模式下启用行号?
- excel - 我在查询中对字符串变量使用什么格式
- android - 网络离线Android后使用WebRtc重新连接呼叫
- c# - 在 .NET 控制台应用程序中,如何获取用户键入命令的命令提示符的路径?