最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete property and its values in all the object - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question edited Apr 23, 2022 at 11:51 Benarab Badi asked Apr 23, 2022 at 11:36 Benarab Badi Benarab Badi 511 silver badge5 bronze badges 3
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Apr 23, 2022 at 11:38
  • Does this answer your question? How do I remove a key from a JavaScript object? – Ahmet Firat Keler Commented Apr 23, 2022 at 11:39
  • duplicate: Javascript delete nested object properties by name – pilchard Commented Apr 23, 2022 at 11:56
Add a ment  | 

3 Answers 3

Reset to default 5

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)

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.

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
}
}
发布评论

评论列表(0)

  1. 暂无评论