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

202
Views
How to replace all nulls with dash in object?

I have an object and for all keys that have a null value I want to replace this null with an empty string (or a dash in my case)?

For example

Obj = {

key1: null,
key2: null,
key3: true,
...
key_n: null

}

Turns into

Obj = {

key1: "-",
key2: "-",
key3: true,
...
key_n: "-"

}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This will work.

const Obj = {
  key1: null,
  key2: null,
  key3: true,

  key_n: null
};

for (const key in Obj) {
  const val = Obj[key];

  if (val === null) {
    Obj[key] = "-";
  }
}
console.log(Obj);

about 4 years ago · Juan Pablo Isaza Report

0

const obj = {
  key1: null,
  key2: null,
  key3: true,
  key_n: null
};
Object
  .entries(obj)
  .forEach(function ([key, value]) {
    if (value === null) {

      this[key] = '-';
    }
  }, obj); // make use of `forEach`'s `thisArg` parameter.

console.log({ obj });

about 4 years ago · Juan Pablo Isaza Report

0

You just have to match whether a key's corresponding value is null or not , if yes then change it to underscore.

Obj = {

    key1: null,
    key2: null,
    key3: true,
    key_n: null

    }

    Object.keys(Obj).map(key=>{
        if(!Obj[key])Obj[key] = "_"
    })
    console.log(Obj)

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!