4

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',
                    }
                ]
            }
        ]
    },
]
1
  • Just a heads up, your working with a Javascript Object.. Although it looks similar JSON is something else. Commented Dec 4, 2019 at 13:09

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.