Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

javascript - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...