How export a nested variable from a function?

Hey guys I wonder how can I export this nested variable or If there isn't, is there another way to handle this? Thanks!
7 Replies
MarkBoots
MarkBoots3y ago
Next time please don't put screenshots of code, use the codeblocks. (read the pinned post in this channel "How to ask good questions") const data = svgToArr(text) will call the function, but to receive the data back, you need to return arr at the end of the function
Electronic
Electronic3y ago
Your declaring arr outside do you need to return it I guess it's just modifying the original arr outside your function
ei
eiOP3y ago
sorry about that and thanks for the help and actually I changed the code to this
const form = document.getElementById('Form')
const inpFile = document.getElementById('inpFile')

let arr = []

form.addEventListener('submit', (e) => {
e.preventDefault()
const file = inpFile.files[0]
const reader = new FileReader()

reader.onload = (e) => {
const text = e.target.result
csvToArr(text);
}

reader.readAsText(file)
})

function csvToArr(text) {
const rows = text.slice(text.indexOf('\n')).split('\n')

for(let i = 0; i < rows.length; i++) {
if(rows[i].length !== 0 || rows[i] !== '') {
arr.push(rows[i])
} else {
console.log('nothing can be found')
}
}

return links(arr)
}

export const links = arr => {
const arrUrls = arr
console.log(arrUrls)
return arrUrls
}
const form = document.getElementById('Form')
const inpFile = document.getElementById('inpFile')

let arr = []

form.addEventListener('submit', (e) => {
e.preventDefault()
const file = inpFile.files[0]
const reader = new FileReader()

reader.onload = (e) => {
const text = e.target.result
csvToArr(text);
}

reader.readAsText(file)
})

function csvToArr(text) {
const rows = text.slice(text.indexOf('\n')).split('\n')

for(let i = 0; i < rows.length; i++) {
if(rows[i].length !== 0 || rows[i] !== '') {
arr.push(rows[i])
} else {
console.log('nothing can be found')
}
}

return links(arr)
}

export const links = arr => {
const arrUrls = arr
console.log(arrUrls)
return arrUrls
}
The problem now if it is possible to get the arrUrls after exporting my links to another js file if I call it there
import { links } from './uploader.js'
console.log(links)
import { links } from './uploader.js'
console.log(links)
😁 tbh I don't know what I'm doing yes I want to return the array and be able to reuse it on another JS file
Electronic
Electronic3y ago
Oo like imports I get it now
ei
eiOP3y ago
yes exactly So in the end, I just want to export this array of links that I get from the CSV and export it from one file to another and be able to reuse it to there🥲
Electronic
Electronic3y ago
export - JavaScript | MDN
The export declaration is used to export values from a JavaScript module. Exported values can then be imported into other programs with the import declaration or dynamic import. The value of an imported binding is subject to change in the module that exports it — when a module updates the value of a binding that it exports, the update will be vi...
ei
eiOP3y ago
I see, thanks man🙌🏼 OVA I need to change my code
Want results from more Discord servers?
Add your server