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

103
Views
Looping data from json using Array

I'm trying to write a function but I doesn't make it. This function works like that

Input: changeSetting("a>b>c","hello")

After that "setting" named value change from {} to {"a":{"b":{"c":"hello"}}}

If input is changeSetting("a","hello") json become {} to {"a":"hello"}

My last code attempt:

function changeSetting(name,val)  {
  if (name.includes(">")) {
    name = name.split('>')
    let json = {}
    name.map((el,i)=>{
    let last = "" 
    name.filter(el=>!name.slice(i+1).includes(el)).map(el=> {
    if(last!="") {
      json[el] = {}
    }})
    }) 
  }
 }

How can we make this ? (Optimization not important but if is it good for me)

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

0

const changeSetting = (setting, target) => {
  if (setting.length < 2) {
    return {
      [setting]: target
    }
  } else {

    const keys = setting.split('>');

    return keys.reduceRight((acc, curr, i) => {

      console.log(acc);

      if(i === keys.length - 1) {
        return acc = {[curr] : target}    
      }
      
        return acc = { [curr]: acc };
    }, {})

    
  }
}


console.log(changeSetting('a', 'hello'));
console.log(changeSetting('a>b>c', 'hello'));
about 4 years ago · Juan Pablo Isaza Report

0

function changeSetting(inputProperties, value) {
  let result;
  const properties = inputProperties.split(">");
  result = `{${properties
    .map((property) => `"${property}":`)
    .join("{")}"${value}"${"}".repeat(properties.length)}`;
  return result;
}
changeSetting("a>b>c", "hello");
changeSetting("a", "hello");

about 4 years ago · Juan Pablo Isaza Report

0

There's multiple ways to do this, I've commented the snippet

const changeSetting = (name, val) => {
    
    // Split and reverse the name letters
    const nameSplit = name.split('>').reverse();

    // Set up the inner most object
    let newObj = {[nameSplit[0]]:val}

    // Now remove the first letter and recurse through the rest
    nameSplit.slice(1).forEach((el, idx) => newObj = {[el]: newObj});

    console.log(newObj);
}

changeSetting("a>b>c", "hello")
changeSetting("a", "hello")
changeSetting("a>b>c>d>e>f>g", "hello")

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!