0

I want to store a CSV file in a variable, which I can use later. Currently, I am using Vue and d3 to do this

let app = Vue.createApp({
            data: function(){
                return{   
          loaddata: {} 
            }
        },
methods:{
    async greet(){
        let data = await d3.csv("a.csv",function(data){d3.select("#output").html(data.map(function(d){return d.c;}));
});
    this.loaddata=data,
    console.log(this.loaddata)
    },
    greet2(){
      for (const {c,b} in this.loaddata){
        console.log(c,b);
      }
    }

    }
})
app.mount('#app') 
  
</script>

greet is just a method that activates when I click enter in my frontend. So basically my end goal is to get variable (array) that I can use with loops to display the CSV in the frontend. If there is another way to load and store a csv file in a variable (array), I will be very grateful if you could let me know that.

I tried searching a lot but couldn't find a solution, I apologize if this is a noob question, I am very new to javascript.

harsh panday
  • 83
  • 1
  • 10
  • `d3.csv` returns a promise, so when you do `let data = d3.csv(etc...`, with or without async/await, what you have is just a promise. You can read about it here: https://stackoverflow.com/q/47664292/5768908. – Gerardo Furtado May 19 '22 at 06:29
  • 1
    @GerardoFurtado That is no fully true: when using it with `await` then `data` will hold the loaded content, **not** a promise. `let data = await (() => Promise.resolve(1))() // data === 1` – altocumulus May 19 '22 at 09:46
  • @altocumulus good to know. – Gerardo Furtado May 20 '22 at 04:07

0 Answers0