Skip to main content
added 61 characters in body
Source Link
Nina Scholz
  • 387.8k
  • 26
  • 367
  • 417

You could use Object.keys and check the values. If found, then the actual path is returned and the iteration stops. If not, all possible pathes are checked.

This proposal respects numeric keys off arrays.

function findPath(a, obj) {
    function iter(o, p) {
        return Object.keys(o).some(function (k) {
            result = p.concat(Array.isArray(o) ? +k : k);
            return o[k] === a || o[k] && typeof o[k] === 'object' && iter(o[k], result);
        });
    }
    var result;
    return iter(obj, []) && result || undefined;
}

var obj = { a: [1, 2, { o: 5 }, 7], b: [0, [{ bb: [0, "str"] }]] };

console.log(findPath(5, obj));     // ["a", 2, "o"]
console.log(findPath("str", obj)); // ["b", 1, 0, "bb", 1]
console.log(findPath(42, obj));    // undefined
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could use Object.keys and check the values. If found, then the actual path is returned and the iteration stops. If not, all possible pathes are checked.

This proposal respects numeric keys off arrays.

function findPath(a, obj) {
    function iter(o, p) {
        return Object.keys(o).some(function (k) {
            result = p.concat(Array.isArray(o) ? +k : k);
            return o[k] === a || o[k] && typeof o[k] === 'object' && iter(o[k], result);
        });
    }
    var result;
    return iter(obj, []) && result || undefined;
}

var obj = { a: [1, 2, { o: 5 }, 7], b: [0, [{ bb: [0, "str"] }]] };

console.log(findPath(5, obj));
console.log(findPath("str", obj));
console.log(findPath(42, obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could use Object.keys and check the values. If found, then the actual path is returned and the iteration stops. If not, all possible pathes are checked.

This proposal respects numeric keys off arrays.

function findPath(a, obj) {
    function iter(o, p) {
        return Object.keys(o).some(function (k) {
            result = p.concat(Array.isArray(o) ? +k : k);
            return o[k] === a || o[k] && typeof o[k] === 'object' && iter(o[k], result);
        });
    }
    var result;
    return iter(obj, []) && result || undefined;
}

var obj = { a: [1, 2, { o: 5 }, 7], b: [0, [{ bb: [0, "str"] }]] };

console.log(findPath(5, obj));     // ["a", 2, "o"]
console.log(findPath("str", obj)); // ["b", 1, 0, "bb", 1]
console.log(findPath(42, obj));    // undefined
.as-console-wrapper { max-height: 100% !important; top: 0; }

deleted 44 characters in body
Source Link
Nina Scholz
  • 387.8k
  • 26
  • 367
  • 417

You could use Object.keys and check the values. If found, then the actual path is returned and the iteration stops. If not, all possible pathes are checked.

This proposal respects numeric keys off arrays.

function findPath(a, obj) {
    function iter(o, p) {
        return Object.keys(o).some(function (k) {
            result = p.concat(Array.isArray(o) ? +k : k);
            return o[k] === a || o[k] && typeof o[k] === 'object' && iter(o[k], result);
        });
    }
    var result = [];result;
    return iter(obj, []) && result;result || undefined;
}

var obj = { a: [1, 2, { o: 5 }, 7], b: [0, [{ bb: [0, "str"] }]] };

console.log(findPath(5, obj));
console.log(findPath("str", obj));
console.log(findPath(42, obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could use Object.keys and check the values. If found, then the actual path is returned and the iteration stops. If not, all possible pathes are checked.

This proposal respects numeric keys off arrays.

function findPath(a, obj) {
    function iter(o, p) {
        return Object.keys(o).some(function (k) {
            result = p.concat(Array.isArray(o) ? +k : k);
            return o[k] === a || o[k] && typeof o[k] === 'object' && iter(o[k], result);
        });
    }
    var result = [];
    return iter(obj, []) && result;
}

var obj = { a: [1, 2, { o: 5 }, 7], b: [0, [{ bb: [0, "str"] }]] };

console.log(findPath(5, obj));
console.log(findPath("str", obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could use Object.keys and check the values. If found, then the actual path is returned and the iteration stops. If not, all possible pathes are checked.

This proposal respects numeric keys off arrays.

function findPath(a, obj) {
    function iter(o, p) {
        return Object.keys(o).some(function (k) {
            result = p.concat(Array.isArray(o) ? +k : k);
            return o[k] === a || o[k] && typeof o[k] === 'object' && iter(o[k], result);
        });
    }
    var result;
    return iter(obj, []) && result || undefined;
}

var obj = { a: [1, 2, { o: 5 }, 7], b: [0, [{ bb: [0, "str"] }]] };

console.log(findPath(5, obj));
console.log(findPath("str", obj));
console.log(findPath(42, obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }

deleted 44 characters in body
Source Link
Nina Scholz
  • 387.8k
  • 26
  • 367
  • 417

You could use Object.keys and check the values. If found, then the actual path is returned and the iteration stops. If not, all possible pathes are checked.

This proposal respects numeric keys off arrays.

function findPath(a, obj) {
    function iter(o, p) {
        return Object.keys(o).some(function (k) {
            var qresult = p.concat(Array.isArray(o) ? +k : k);
            ifreturn (o[k] === a) {
                result = q;
                return true;
            }
            if|| (o[k] && typeof o[k] === 'object') {
                return&& iter(o[k], qresult);
            }
        });
    }
    var result = [];
    ifreturn (iter(obj, [])) {
        return&& result;
    }        
}

var obj = { a: [1, 2, { o: 5 }, 7], b: [0, [{ bb: [0, "str"] }]] };

console.log(findPath(5, obj));
console.log(findPath("str", obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could use Object.keys and check the values. If found, then the actual path is returned and the iteration stops. If not, all possible pathes are checked.

This proposal respects numeric keys off arrays.

function findPath(a, obj) {
    function iter(o, p) {
        return Object.keys(o).some(function (k) {
            var q = p.concat(Array.isArray(o) ? +k : k);
            if (o[k] === a) {
                result = q;
                return true;
            }
            if (o[k] && typeof o[k] === 'object') {
                return iter(o[k], q);
            }
        });
    }
    var result = [];
    if (iter(obj, [])) {
        return result;
    }        
}

var obj = { a: [1, 2, { o: 5 }, 7], b: [0, [{ bb: [0, "str"] }]] };

console.log(findPath(5, obj));
console.log(findPath("str", obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could use Object.keys and check the values. If found, then the actual path is returned and the iteration stops. If not, all possible pathes are checked.

This proposal respects numeric keys off arrays.

function findPath(a, obj) {
    function iter(o, p) {
        return Object.keys(o).some(function (k) {
            result = p.concat(Array.isArray(o) ? +k : k);
            return o[k] === a || o[k] && typeof o[k] === 'object' && iter(o[k], result);
        });
    }
    var result = [];
    return iter(obj, []) && result;
}

var obj = { a: [1, 2, { o: 5 }, 7], b: [0, [{ bb: [0, "str"] }]] };

console.log(findPath(5, obj));
console.log(findPath("str", obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Source Link
Nina Scholz
  • 387.8k
  • 26
  • 367
  • 417
Loading