0

I am scraping a website for data and to makes the final result when I push an object into a javascript list it replaces all the pre-existing values with the last value pushed.

when I console.logged it right before pushing it I got the correct answer.

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)
})

the result comes out to be

{ 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' } ] }

all of them should be diffrent

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
ASHUTOSH SINGH
  • 131
  • 1
  • 13
  • 3
    Possible duplicate of [javascript .push() inserts the same object every time](https://stackoverflow.com/questions/23395602/javascript-push-inserts-the-same-object-every-time) – CertainPerformance Jan 08 '19 at 10:37
  • What does rp() function do? If this a request to the url you are running it asynchronously. Therefore your return value is 'undefined' –  Jan 31 '19 at 20:08
  • i have modified my request function to throw back a promise making it synchronous i found the sol to it it was a problem with scoping . – ASHUTOSH SINGH Feb 06 '19 at 14:08
  • The massive number of global variables here is concerning. Always use `const` or `let` in front of variables. Please use prettier to format your code. – ggorlen Jan 03 '23 at 23:18

0 Answers0