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

216
Views
Get a value from an object and use that value to fill up other keys in that same object under a condition

So here's an example:

{
    "part": "Intro",
    "e": "------5/6------8\\6-|-------------------|-------------------",
    "B": "-----------9-------|---------6p8---(6)-|-------------------",
    "G": "--8----------------|---8h9-------------|--<8>--------------",
    "D": "",
    "A": "",
    "E": "",
    "endMsg": "Continue..."
}

Note: The double-slash will turn into one slash only upon render of text.

  1. I want to get value from this object that is not empty. (So that could be from the e key or from the B key. As long it's not empty.)

  2. Then I'm replacing that value using this expression here:

    str.replace(/[0-9-. a-zA-Z // \ ~ ( ) < >]/g, '-');

It's for replacing the numbers, letters, and other characters into dashes.

  1. I want to use that value with the dashes and pipe chars only to fill up the other keys inside the same object that are empty.
  2. In the end, I want it to look something like this:
{
   "part": "Intro",
   "e": "------5/6------8\\6-|-------------------|-------------------",
   "B": "-----------9-------|---------6p8---(6)-|-------------------",
   "G": "--8----------------|---8h9-------------|--<8>--------------",
   "D": "-------------------|-------------------|-------------------",
   "A": "-------------------|-------------------|-------------------",
   "E": "-------------------|-------------------|-------------------",
   "endMsg": "Continue..."
}

I have no idea how to achieve this in code. Please help.

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

0

You can loop over each key value pair and replace the value if its empty. Be aware that a backslash is used for escaping a character. To fix this we first replace the backslashes with dashes and then replace the rest.

The regex could also be simplified to /[^-|]/g which means replace all symbols except - and |

const lines = {
    "part": "Intro",
    "e": "------5/6-----8\\6-|-------------------|-------------------",
    "B": "-----------9-------|---------6p8---(6)-|-------------------",
    "G": "--8----------------|---8h9-------------|--<8>--------------",
    "D": "",
    "A": "",
    "E": "",
    "endMsg": "Continue..."
};

const createFullLines = (lines, blacklist = ['part', 'endMsg']) => {
  // Find a line that is not empty
  const line = Object.entries(lines).find(([key, line]) => {
    return !blacklist.includes(key) && line.trim();
  });
  
  // Exit if all lines are empty
  if(!line) return lines;
  
  // Destructure to get only value
  const [_, filledLine] = line;
  
  // Create new line with only dashes
  const newLine = filledLine.replace(/[^-|]/g, '-');
  
  // Update lines
  for(const key in lines) {
    lines[key] ||= newLine;
  }
  
  return lines;
}

const result = createFullLines(lines);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You should just iterate between your object's keys like this:

// find first non-empty field, !! - conversion to boolean
let nonEmptyKey = Object.keys(obj).find(key => !!obj[key]);

for(let key of Object.keys(obj)) {
  // check if value is empty
  if(!obj[key]) {
    obj[key] = obj[nonEmptyKey].replace(/[0-9-. a-zA-Z // \ ~ ( ) < >]/g, '-');
  }
}

Also, you can simplify your regex to this:

// replace all symbols except - and |
str.replace(/[^-|]/g, '-');
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!