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

122
Visualizações
How to change a Javascript object with a dynamic path?

Im trying to change a variable list with newList. The problem is that i dont know how to change the list when it got path added with a function.

Add string to object

Object.byString = function(o, s) {
    s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
    s = s.replace(/^\./, '');           // strip a leading dot
    var a = s.split('.');
    for (var i = 0, n = a.length; i < n; ++i) {
        var k = a[i];
        if (k in o) {
            o = o[k];
        } else {
            return;
        }
    }
    return o;
}

Return path

function getPath2(){
   const split = get("path").split('~');
   var newSplit = "";
   for (key in split){
      if(!split[key] == ""){  
        newSplit = newSplit + "."+split[key]+"";
      }
   }
   return newSplit;
   }
//  this works
//  {afas: {…}, fa: {…}, fas: {…}, af: {…}}
console.log(Object.byString(list, getPath2()));

//  this works
// {afas: {…}, fa: {…}, af: {…}, fas: {…}}
console.log(newList);

// This is not working
Object.byString(list, newPath) = newList;

Error I get back: script.js:420 Uncaught ReferenceError: Invalid left-hand side in assignment at HTMLLIElement. (script.js:420:32)

about 4 years ago · Juan Pablo Isaza
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