DEV Community

Discussion on: Find Value at Key

Collapse
 
frankwisniewski profile image
Frank Wisniewski

I don't understand why you need that.
a simple:

console.log(obj.a.b.c)
Enter fullscreen mode Exit fullscreen mode

gives the same result.

Collapse
 
curiousdev profile image
CuriousDev

Yes, but I guess it is more like a challenge.

Collapse
 
peerreynders profile image
peerreynders

When you are dealing with data where the contents may vary and you don't want to necessarily run into Uncaught TypeError: Cannot read properties of undefined

If the path is fixed at design time in modern JavaScript you can use the Optional chaining (?.) and Nullish coalescing operator (??) operator to avoid errors.

const text = '{"a":{"b":{"c":12,"j":false},"k":null}}';
const data = JSON.parse(text);

console.log(data?.a?.b?.c); // 12
console.log(data?.a?.b); // {c: 12, j: false}
console.log(data?.a?.b?.d ?? 'defaultValue'); // "defaultValue"
// console.log(data.a.b.d.e); // Uncaught TypeError: Cannot read properties of undefined (reading 'e')"
Enter fullscreen mode Exit fullscreen mode

When the path is generated at runtime. lodash supports this functionality with result and get. obj.a.b.c can only be specified at design time - i.e. when you write the code. Dealing with "self-describing data" it is sometimes necessary to formulate a path programmatically at runtime and then access (and/or mutate) it.

const text = '{"a":{"b":{"c":12,"j":false},"k":null}, "target":"a.b.c"}';
const data = JSON.parse(text);
const other = { a: { ...JSON.parse(text).a, d: { c: 0 } }, target: 'a.d.c' };

increment(data);   // i.e. increment at a.b.c
increment(other);  // i.e. increment at a.d.c

// ... just checking
console.log(data.a.b.c);   // 13
console.log(other.a.d.c);  //  1

function increment(data) {
  const path = data.target;
  const value = _.get(data, path);
  _.set(data, path, typeof value === 'number' ? value + 1 : 0);
}
Enter fullscreen mode Exit fullscreen mode