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

254
Views
How can I pretty print keys and values of a JavaScript object on web page

I have a JavaScript object that contains terms and definitions. I want to create a function that displays/prints the key, value pairs on a webpage.

For example:

let dict = {
    term1: "definition",
    term2: "definition",
    term3: "definition",
    term4: "definition"
}

Ideal output:

term1: definition

term2: definition

term3: definition

term4: definition

I am currently using this code:

function viewAll()
{
var json = JSON.stringify(dict,null,3);
document.getElementById("output").innerText = json;
}

which outputs:

{
term1: "definition",
term2: "definition",
term3: "definition",
term4: "definition"
}

This is okay, but I'd like to remove the special characters ({",)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Us Object.entries and map to format the data how you want to display it.

let dict = {
    term1: "definition 1",
    term2: "definition 2",
    term3: "definition 3",
    term4: "definition 4"
}

const str = Object.entries(dict).map(([key, value]) => `${key}: ${value}`).join("<br/>");

document.getElementById("out").innerHTML = str;
<div id="out"></div>

Seems like you really want a definition list

let dict = {
  term1: "definition 1",
  term2: "definition 2",
  term3: "definition 3",
  term4: "definition 4"
}

const str = Object.entries(dict).map(([key, value]) => `<dt>${key}</dt><dd>${value}</dd>`).join('');

document.getElementById("out").innerHTML = str;
dt::after {
  content: ":"
}

dt {
  display: inline-block;
}

dd {
  display: inline;

}

dd:after {
  content: '';
  display: block;
}
<dl id="out"></dl>

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!