Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda