getPathValue() function for deep objects with arrays and with packed JSON

雨燕双飞 提交于 2019-12-01 23:11:26

You could replace the brackets and take the remaining values as keys. Inside of the reducing, you could use a default object for not given objects.

function getValue(object, path) {
    return path
        .replace(/\[/g, '.')
        .replace(/\]/g, '')
        .split('.')
        .reduce(function (o, k) {
            return (typeof o === 'string' ? JSON.parse(o) : (o || {}))[k];
        }, object);
}
var object = {"id":"0001","type":"donut","name":"Cake","ppu":0.55,"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"}]},"data":"{\"domain\":\"cooking.com\",\"id\":53819390}"},
    path = 'batters.batter[1].id';

console.log(getValue(object, path));

The following would do the job using a Regex on each value :

const data = {
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
    "batters": {
        "batter": [
            {
                "id": "1001",
                "type": "Regular"
            },
            {
                "id": "1002",
                "type": "Chocolate"
            }
        ]
    },
    "data": "{\"domain\":\"cooking.com\",\"id\":53819390}"
}

function getValue(object, path) {
    return path
        .split('.')
        .reduce(function (o, k) {
            const indexSearch = (new RegExp(/\[([0-9]*)\]/)).exec(k)
            const index = indexSearch ? indexSearch[1] : null
            k = k.replace(/\[[0-9]*\]/, '')
            const sub = (typeof o === 'string' ? JSON.parse(o) : o)[k]
            return index ? sub[index] : sub;
        }, object);
}

console.log(getValue(data, 'batters.batter[1]'))
console.log(getValue(data, 'data.domain'))
console.log(getValue(data, 'batters.batter[1].id'))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!