Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search a string in JSON using Typescript

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',
                    }
                ]
            }
        ]
    },
]
like image 831
Nithin Benny Avatar asked Oct 16 '25 14:10

Nithin Benny


1 Answers

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; }
like image 73
Nina Scholz Avatar answered Oct 18 '25 09:10

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!