【问题标题】:Search a string in JSON using Typescript使用 Typescript 在 JSON 中搜索字符串
【发布时间】:2020-03-29 06:25:09
【问题描述】:

使用 Typescript 在 JSON 中搜索字符串。如何在下面给定的 JSON 数组中搜索字符串? JSON 输入数组如下所示。

let JSON_DATA: Object[] = [
    {
        id: "1",
        name: 'cricket',
        subNames: [
            { 
                id: 2, 
                name: 'bat',
                subNames: [
                    { 
                        id: 3, 
                        name: 'batsman',
                        subNames: [
                            { 
                                id: 4, 
                                subNames: 'left',
                            }
                        ]
                    }
                ]
            },
            { 
                id: , 
                name: 'ball',
                subNames: [
                    { 
                        id: 6, 
                        subNames: 'red',
                    },
                    { 
                        id: 7, 
                        name: 'white',
                    }
                ]
            }
        ]
    },
    {
        id: 8,
        name: 'football',
        subNames: [
            { 
                id: 9, 
                name: 'boot',
                subNames: [
                    { 
                        id: 10, 
                        name: 'white',
                    }
                ]
            }
        ]
    },
]

我想在上面的 JSON 对象中搜索一个字符串

例如,如果用户键入“white”,那么它应该在整个 JSON 数组中搜索字符串“white”并返回如下 JSON 对象...

let JSON_DATA: Object[] = [
    {
        id: "1",
        name: 'cricket',
        subNames: [
            { 
                id: , 
                name: 'ball',
                subNames: [
                    { 
                        id: 7, 
                        name: 'white',
                    }
                ]
            }
        ]
    },
    {
        id: 8,
        name: 'football',
        subNames: [
            { 
                id: 9, 
                name: 'boot',
                subNames: [
                    { 
                        id: 10, 
                        name: 'white',
                    }
                ]
            }
        ]
    },
]

【问题讨论】:

  • 请注意,您使用的是 Javascript 对象。虽然它看起来很相似,但 JSON 是另一回事。

标签: javascript arrays json typescript


【解决方案1】:

将此代码作为基本代码尝试

interface Item {
    id: string|number;
    name?: string;
    subNames?: null|Item[];
}


function search(data: Item|Item[], s: string): Item[] {
    let result: Item[] = [];
    if (data instanceof Array) {
        for (let i = 0; i < data.length; i++) {
            result = result.concat(search(data[i], s));
        }
    } else {
        if (data.subNames instanceof Array) {
            result = result.concat(search(data.subNames, s));
        } else {
            if (data.name === s) {
                result = result.concat(data);
            }
        }
    }
    return result;
}

console.log(

    search([
        {
            id: 8,
            name: 'football',
            subNames: [
                {
                    id: 9,
                    name: 'boot',
                    subNames: [
                        {
                            id: 10,
                            name: 'white',
                        }
                    ]
                }
            ]
        }
    ], 'white')
    
);

【讨论】:

    【解决方案2】:

    在纯 Javascript 中,您可以先查找值或检查 subNames 及其嵌套出现。

    function find(array, string) {
        return array.reduce((r, o) => {
            if (Object.values(o).some(v => v === string)) {
                r.push(o);
                return r;
            }
            if (Array.isArray(o.subNames)) {
                var subNames = find(o.subNames, string);
                if (subNames.length) r.push(Object.assign({}, o, { subNames }));
            }
            return r;
        }, []);
    }
    
    var data = [{ id: 1, name: 'cricket', subNames: [{ id: 2, name: 'bat', subNames: [{ id: 3, name: 'batsman', subNames: [{ id: 4, subNames: 'left' }] }] }, { id: 4, name: 'ball', subNames: [{ id: 6, subNames: 'red' }, { id: 7, name: 'white' }] }] }, { id: 8, name: 'football', subNames: [{ id: 9, name: 'boot', subNames: [{ id: 10, name: 'white' }] }] }];
    
    console.log(find(data, 'white'));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:

      我写了一个非常简单的库,你可以使用ss-search

      代码如下所示:

      search(JSON_DATA, ["name", "subNames[name]", "subNames[subNames].subNames[name]"], "SEARCH_STRING")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-21
        • 2013-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多