Skip to content Skip to sidebar Skip to footer

Better / Faster Way To Modify All Matched Property Found Under Different Depth From A Nesting Object

I receive a json from an API that I need to parse and modify one property value. Thing is, the nesting structure of the json data I receive are inconsistent and I have no control o

Solution 1:

You can pass a second parameter to JSON.parse that recursively transforms all desired property values:

var parsedJson = JSON.parse(
  '{"a":[{"a1":[{"p":0},{"np":1}]}],"b":[{"p":0},{"np":1}],"c":[{"c1":[{"c2":[{"p":0}]},{"np":1}]}]}',
  (key, val) => key === 'p' ? 1 : val
);
console.log(parsedJson);

Post a Comment for "Better / Faster Way To Modify All Matched Property Found Under Different Depth From A Nesting Object"