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

70
Views
Find the object key that's not used in a replace function

I have a replace function that takes the values from an object and replaces the template literals like this:

let componentJSON = [
  { "template": "<div class='${layout} ${border}'></div>" },
  {
    "layout": "grid",
    "color": "blue",
    "border": "primary"
  }
];

const template = componentJSON[0].template
const classes = componentJSON[1]

let html = template.replace(/\$\{(.*?)\}/g, (match, key) => classes[key]);

console.log(html);

Is it possible to then return (or consoleLog) the object key that wasn't used? In this case "color": "blue"

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

0

I suppose you could delete the property from the object, then examine what remains in the object at the end.

let componentJSON = [
  { "template": "<div class='${layout} ${border}'></div>" },
  {
    "layout": "grid",
    "color": "blue",
    "border": "primary"
  }
];

const template = componentJSON[0].template
const classes = componentJSON[1]

const html = template.replace(
  /\$\{(.*?)\}/g,
  (match, key) => {
    const str = classes[key];
    delete classes[key];
    return str;
  }
);

console.log(componentJSON[1]);

about 4 years ago · Juan Pablo Isaza Report

0

Keep track of the keys used then filter them from the original key set

let componentJSON = [
  { "template": "<div class='${layout} ${border}'></div>" },
  {
    "layout": "grid",
    "color": "blue",
    "border": "primary"
  }
];

const [ { template }, classes ] = componentJSON;

const usedKeys = new Set();
let html = template.replace(/\$\{(.*?)\}/g, (match, key) => 
  (usedKeys.add(key), classes[key]));

console.log(html);

const unused = Object.fromEntries(
  Object.entries(classes).filter(([ key ]) => !usedKeys.has(key))
);

console.log("unused", unused);

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!