簡體   English   中英

使用 Typescript 在 JSON 中搜索字符串

[英]Search a string in JSON using Typescript

使用 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 中,您可以先查找值或檢查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; }

試試這個代碼作為基本

 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') );

我寫了一個非常簡單的庫,你可以使用ss-search

代碼如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM