'Search nested JS object using object-scan lib to find grand parent elements

Using Object-Scan JS lib, want to find all the grand parent elements that have key "referenceField". Any help is much appreciated.

JS Fiddle Link

const find = (item, input) => objectScan(['**'], {
    abort: true,
    rtn: 'parent',
    filterFn: ({
      key
    }) => key === item.key
  })(input);

console.log(find('referenceField', data));


Solution 1:[1]

Object-scan was somehow not returning grand parent using gparent option, but based on the assumption that your properties will always have the same nesting, you can take help of parents option and change your code to this-

const find = (item, input) => objectScan([`**.${item}`], {
    abort: true,
    rtn: 'parents'
})(input)[1];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1