javascript - Search a string in JSON using Typescript - Stack Overflow

Search a string in JSON using Typescript. How can I search for a string in the below given JSON array?

Search a string in JSON using Typescript. How can I search for a string in the below given JSON array? The JSON input array is given below.

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',
                    }
                ]
            }
        ]
    },
]

I want to search a string in the above JSON object

For example, if a user types 'white' then it should search for the string 'white' in the whole JSON array and return the JSON object like below...

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',
                    }
                ]
            }
        ]
    },
]

Search a string in JSON using Typescript. How can I search for a string in the below given JSON array? The JSON input array is given below.

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',
                    }
                ]
            }
        ]
    },
]

I want to search a string in the above JSON object

For example, if a user types 'white' then it should search for the string 'white' in the whole JSON array and return the JSON object like below...

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',
                    }
                ]
            }
        ]
    },
]
Share edited Dec 5, 2019 at 3:22 zapping 4,1166 gold badges40 silver badges56 bronze badges asked Dec 4, 2019 at 12:35 Nithin BennyNithin Benny 1421 silver badge7 bronze badges 1
  • Just a heads up, your working with a Javascript Object.. Although it looks similar JSON is something else. – Keith Commented Dec 4, 2019 at 13:09
Add a ment  | 

3 Answers 3

Reset to default 3

In plain Javascript, you could look for a value first or have a check for subNames and their nested occurences.

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; }

Try this code as basic

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

I have written a very simple library you can use ss-search

The code would look like this:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744820310a4595574.html

相关推荐

  • javascript - Search a string in JSON using Typescript - Stack Overflow

    Search a string in JSON using Typescript. How can I search for a string in the below given JSON array?

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信