Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path to matching key value pair in a nested object

I was trying to implement a search in a nested object.

   // Returns an array of matching objects
    function getObjects(obj, key, val) {
        var objects = [];
        for (var i in obj) {
            if (!obj.hasOwnProperty(i)) continue;
            if (typeof obj[i] == 'object') {
                objects = objects.concat(getObjects(obj[i], key, val));
            } else if (!$.isNumeric(obj[key]) && i == key && obj[key].toLowerCase().match(val)) {
                objects.push(obj);
            }
        }
        return objects;
    }

This function returns the matching object on basis of key value pair provided.

What I want is the path to the object where key value pair is found.

Sample data

TestObj = {
    "Categories": [{
        "Product1": [{
            "id": "a01",
            "name": "Pine",
            "description": "Short description of pine."
        }, {
            "id": "a02",
            "name": "Pine",
            "description": "Short description of pine."
        }, {
            "id": "a03",
            "name": "Poplar",
            "description": "Short description of poplar."
        }],
        "id": "A",
        "title": "Cheap",
        "description": "Short description of category A."
    }, {
        "Product2": [{
            "id": "b01",
            "name": "Maple",
            "description": "Short description of maple."
        }, {
            "id": "b02",
            "name": "Oak",
            "description": "Short description of oak."
        }, {
            "id": "b03",
            "name": "Bamboo",
            "description": "Short description of bamboo."
        }]
    }]
};

I was trying to write a function

function objPath(obj, key, val, path) {
    var result = [];
    var passName = '';
    if (path) {
        passName = path;
    }
    var tempArray = [];
    for (var prop in obj) {
        var value = obj[prop];
        if (typeof value === 'object') {
            tempArray = objPath(value, key, val, passName);
            $.each(tempArray, function (k, value) {
                result.push(value);
            });
        } else if (prop == key && obj[key].toLowerCase().match(val)) {
            result.push(obj[key]);
        }
    }
    return result;
}

If I call function as

objPath(TestObj, 'id', 'b03');

Which should return Categories > Product2 > 3rd Row

But all I'm getting is the key. How to fix the objPath function to obtain required result

like image 710
Okky Avatar asked Nov 02 '22 10:11

Okky


1 Answers

I have wrote a custom function

function objPath(obj, key, val, path) {
    var result = [];
    var passName = '';
    if (path) {
        passName = path;
    }
    var tempArray = [];
    for (var prop in obj) {
        var value = obj[prop];
        if (typeof value === 'object') {
            tempArray = objPath(value, key, val, passName);
            $.each(tempArray, function (k, value) {
                result.push(value);
            });
        } else if (!$.isNumeric(obj[key]) && prop == key && obj[key].toLowerCase().match(val)) {
            result.push(passName + '["' + obj[prop] + '"]');
        } else {
            if ($.isNumeric(obj[prop])) {
                //passName += ' > ' + obj[prop];
            } else {
                passName += '["' + obj[prop] + '"]';
            }
        }
    }
    return result;
}

Which will return

["Categories"]["Product2"]["b03"]
like image 68
Okky Avatar answered Nov 15 '22 05:11

Okky