Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
Ejs variables in script tag/ setting array to variable become string

So i needed to pass a variable to the script tag in the ejs file. it's question array (i called it object in the var name for no reason), that i need to work with in the script tag. i saw that it's done by '<%-varname%>' . But the thing is, i want to remove stuff from the array while working with it, so i need a copy of this array. And here is the problem:

console.log('<%-passedUser.questionsObject%>')
console.log('<%-passedUser.questionsObject[0]%>')
arrayToUse = '<%-passedUser.questionsObject%>'
console.log(arrayToUse)
console.log(arrayToUse[0])

Output:

question1,answer1,answer2,answer3,answer4,answer5,q2,answer1,answer2 

question1,answer1,answer2,answer3,answer4,answer5 

question1,answer1,answer2,answer3,answer4,answer5,q2,answer1,answer2 

q

I tried JSON.stringify, parse, nothing works. i'd appreciate any help.

Note: i don't know why '<%-passedUser.questionsObject%>' isn't logging the correct way, but it's giving correct values. It's true form is :

[[question1,answer1,answer2,answer3,answer4,answer5],[q2,answer1,answer2]]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you have to use both json parse and json stringify

arrayToUse = JSON.parse('<%- JSON.stringify(passedUser.questionsObject)%>')

later you can easily split the array using

const [questions,answers] = arrayToUse

or do it before sending it to the client

<% const [questions,answers] = passedUser.questionsObject %>

<script>
  const  arrayToUse = JSON.parse('<%- JSON.stringify(questions)%>')
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!