Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda