JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//wzrd.in/standalone/expect@latest"></script>
<script src="//wzrd.in/standalone/deep-freeze@latest"></script>
<body>
</body>
Babel + JSX
const search = (obj, target, path = []) => {
let paths = [];
for (let i in obj) {
if (obj.hasOwnProperty(i)) {
let ref = obj[i];
if (ref === target) {
paths.push([...path, i].join('.'));
}
if (typeof ref === 'object') {
let results = search(ref, target, [...path, i]);
if (results.length > 0) {
paths = paths.concat(results);
}
}
}
}
return paths;
};
const obj = {
a: {
b: {
c: 1
}
},
d: 2,
e: {
f: 3
},
g: 1
};
deepFreeze(obj);
expect(search(obj, 1))
.toEqual(['a.b.c', 'g']);
document.querySelector('body').innerHTML = 'All tests passed';