8

I got this path array:

const path = ["a", "b", "c", "d"]

and an object like:

let obj = { "a": { "b": { "c": { "d": 10, "e": 20 } } } }

and I would like to delete that obj["a"]["b"]["c"]["d"] so my obj will be

{ "a": { "b": { "c": { "e": 20 } } } }

I've tried to do some path.forEach looping, appending it but couldn't find a proper way which I appended it as I wish to, so I could access obj to the right place.

4 Answers 4

19

You could do this recursively and then use delete to remove the final key-value pair in your base-case:

const path = ["a", "b", "c", "d"]
const obj = { "a": { "b": { "c": { "d": 10, "e": 20 } } } };

const remove_from_path = (obj, [prop, ...rest]) => 
  !rest.length ? delete obj[prop] : remove_from_path(obj[prop], rest);

remove_from_path(obj, path);
console.log(obj);

Sign up to request clarification or add additional context in comments.

6 Comments

Took a while to understand the second argument of the function, and I must admit it's incredibly clever. Definitely interesting.
Apparently, It's more important to go for complicated code, that it is to go for efficient code. The difference in efficiency between your and my snippet is less than 5%, so it's negligible. What I don't get is why there is a such a huge difference in upvotes
@nickzoum No one said that. In fact, your code is much faster, but a for loop would be even faster. Alas, the solutions provided here are shorter, concise, and readable compared to a for loop. I wouldn't say its more important to go for complicated code, rather, the bias leans more toward readable, shorter code - which happens to be complicated
If your not familiar with the syntax, your bound to be unfamiliar with what it does. You could say this about anything. I use destructuring all the time, so it's readable to me. I guess it just boils down to experience, but I have found that efficiency flies out of the window when writing answers. I've started to write short answers, followed by efficient answers along with a jsperf, which I feel may help newer users and those looking for faster code.
@nickzoum I agree with Kobe, we can always get better with our programming knowledge, through learning of something new, which was unfamiliarity, but become familiar. hence i've accepted this answer. Your answer is great aswell and I do thank you for it. but for me this one gave did me a good example for using destructuring, and after understanding destructuring, the answer is not complicated.
|
5

You can loop through the path array and in each iteration access that property of the obj object. In the last iteration, instead of entering the last property, delete it.

var path = ["a", "b", "c", "d"];
var obj = { "a": { "b": { "c": { "d": 10, "e": 20 } } } };

path.reduce(function(result, key, index) {
  if (index === path.length - 1) delete result[key];
  else return result[key];
}, obj);

console.log(obj);

Comments

5

You could save the last key and reduce the objects to the final object.

const
    remove = (object, [...keys]) => {
        const last = keys.pop();
        delete keys.reduce((o, k) => o[k] || {}, object)[last];
    },
    path = ["a", "b", "c", "d"],
    obj = { a: { b: { c: { d: 10, e: 20 } } } };


remove(obj, path);
console.log(obj);

Comments

0

let obj = { "a": { "b": { "c": { "d": 10, "e": 20 } } } };
const path = ["a", "b", "c", "d"];

const recursiveLookup = (obj) => {
  path.forEach((el) => {
    if(typeof obj[el] === 'object'){
      recursiveLookup(obj[el])
    } else {
      delete obj[el];
    }
  });
};
recursiveLookup(obj);

console.log(obj)

3 Comments

Why typeof obj[el] === 'object'? What if "d" was an Object? What if it was null?
it is not working if you change the path to ["a", "b", "c"] or ["a", "b"]
It's gonna work only for this specific case. Indeed if "d" would have been null then this would have failed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.