0

I have data structure similar like this

{"id": 1, "a": [{"id": 2, "b": [{"id": 3, "c": [{"id": 4}]}]}]}

I would like to change every id to null.

I know I could do loops and manually assign null to id. Is there any easy way to do this in JavaScript?

1
  • 3
    JSON.parse(JSON.stringify(x),(k,v)=>k==='id'?null:v); - of course, this doesn't mutate the original - but you didn't specify if you wanted to mutate in place or not Commented Jul 29, 2021 at 13:41

2 Answers 2

2

Without changing the object to a JSON string, you could use recursion:

function clearIds(obj) {
    if ("id" in Object(obj)) obj.id = null;
    Object.values(Object(obj)).forEach(clearIds);
}

// Demo
let obj = {"id": 1, "a": [{"id": 2, "b": [{"id": 3, "c": [{"id": 4}]}]}]};
clearIds(obj);
console.log(obj);

This mutates the given object in place.

For fun: the same function as a one-liner:

const clearIds = o => "id" in Object(o) && (o.id = null) || Object.values(Object(o)).forEach(clearIds);

// Demo
let obj = {"id": 1, "a": [{"id": 2, "b": [{"id": 3, "c": [{"id": 4}]}]}]};
clearIds(obj);
console.log(obj);

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

2 Comments

oh, now that's not fair :p an in-place one-liner - I can't upvote you twice!!!
Thanks a lot @trincot, this is really definitely helpful. You are a life saver. What a solution by one liner!!
2

Bravo's comment was illuminating so I hope they don't mind if I expand on it here.

  1. Turn the object into a string.

  2. Parse it back into an object again.

What I didn't know is that JSON.parse has a "reviver" function that allows you to transform the string value as it's being parsed.

const data = {"id": 1, "a": [{"id": 2, "b": [{"id": 3, "c": [{"id": 4}]}]}]};

// Turn the data into a string
const str = JSON.stringify(data);

// Parse the string back to an object
// but using the reviver function to turn the value of any id
// keys to null
const newObj = JSON.parse(str, (key, value) => key === 'id' ? null : value);

console.log(newObj);

4 Comments

All good - I wasn't sure if OP wanted to change in place or not - change in place would be different - recursively dive into the object - but not that hard either. I just like one liners
Yeah, I just didn't know about the reviver function and thought it was fascinating.
useful for dates in WCF when you want to (need to) bypass loading microsofts cruft for using WCF - if that makes any sense - in JSON format, dates are "encoded" like "/Date(1234567890)/" - I also use the stringify replacer for sending dates to WCF
Thanks a lot @Andy, this is really definitely helpful. I have also just thought of converting it to string and use regex to replace id values with null. But your solution doesn't have to use regex. Good Job! And yea, the output is correct. It's what i want.

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.