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

216
Visualizações
Delete property and its values in all the object

I'm a beginner in javaScript, I have this object MyGraph:

const MyGraph = {
    a: { b: 5, c: 2 },
    b: { a: 5, c: 7, d: 8 },
    c: { a: 2, b: 7, d: 4, e: 8 },
};

I want to delete property "a" and its values in other properties as well to get this result:

const MyGraph = {
    b: { c: 7, d: 8 },
    c: { b: 7, d: 4, e: 8 },
};

I tried like this:

for(let XXX of Object.keys(MyGraph)){
    console.log(XXX.a);
    delete XXX.a;
}

the result of execution:

undefined
undefined
undefined

any help!

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You could use a recursive algorithm :

function del_entries(key, obj) {
  if (obj.hasOwnProperty(key)) {
    delete obj[key];
  }

  // Or with Object.hasOwn, not fully supported by old browsers but more up to date
 /*
 if (Object.hasOwn(obj, key)) {
     delete obj[key]
 }
 */
  
  Object.values(obj).forEach(o=> del_entries(key, o))
}


const MyGraph = {
    a: { b: 5, c: 2 },
    b: { a: 5, c: 7, d: 8 },
    c: { a: 2, b: 7, d: 4, e: 8 },
};

del_entries("a", MyGraph);

console.log(MyGraph)

about 4 years ago · Santiago Trujillo Relatório

0

In your code XXX is the key. You need to do graph[XXX] to access the actual object. So instead of XXX.a you should do graph[XXX].a. But this only accounts for objects in graph that have an the key a. You also need to account for key a in graph. Please see the code below. Its a rudimentary example.

If you have one level of nesting then you can use then you can use the code below.

const mygraph = {
  a: { b: 5, c: 2 },
  b: { a: 5, c: 7, d: 8 },
  c: { a: 2, b: 7, d: 4, e: 8 },
};

console.log(mygraph);

function deletePropAndValuesOf(key, graph) {

  for (const k of Object.keys(graph)) {
    if (k === key) {
      delete graph[key];
    } else {
      if (key in graph[k]) {
        delete graph[k][key]
      }
    }
  }
}

deletePropAndValuesOf("a", graph);

console.log(mygraph);

You can copy the code to a .js file and run it using node. e.g. enter image description here

about 4 years ago · Santiago Trujillo Relatório

0

Ive used object destructuring to remove the first array with an a, but could not figure out how to do all the a's's but the code below might help?

const MyGraph = {
a: { b: 5, c: 2 },
b: { a: 5, c: 7, d: 8 },
c: { a: 2, b: 7, d: 4, e: 8 }};
const {a, ...newMyGraph} = MyGraph;
// output
console.log(newMyGraph)

returns

b: {
   a: 5,
   c: 7,
   d: 8
},
c: {
   a: 2,
   b: 7,
   d: 4,
   e: 8
}
}
about 4 years ago · Santiago Trujillo 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