2

I want to get longest name value in bellow associative array of TypeScript. Some objects in array have child array. I want to check all nest array.

    interface Tree {
    id: string
    name: string
    child?: Tree[]
}
const treeArray: Tree[] = [
    {
        id: "test", name: "test1", child: [
            { id: "Test#2", name: "@2ndtest" },
            {
                id: "test#2", name: "2ndtest", child: [
                    { id: "Test#2#3", name: "@3rdtestTestTest" },
                ]
            }
        ]
    },
    { id: "testtest", name: "testtest2" }
]

So, my expected out put was like below.

{ id: "Test#2#3", name: "@3rdtestTestTest" }

I made code below get output.

    const getLongestNameObjectInTreeArray = (value: Tree[]) => {
    let getObj: Tree = { id: "", name: "" }
    let longestName = 0
    value.forEach((v) => {
        let nameLength = v.name.length
        if (<any>nameLength > longestName) {
            longestName = <any>nameLength
            getObj = { id: v.id, name: v.name }
        }
    })
    return getObj
}
console.log(getLongestNameObjectInTreeArray(treeArray))

but this code output was { id: 'testtest', name: 'testtest2' }

The problem is my function is not loop nest array. Does anyone advise me,please?

2 Answers 2

2

You could use recursion like this:

function getLongestName(tree:Tree):string{
    let treeName = tree.name;
    let longestChildName = tree.child?.reduce((a,b)=>{
        let bName=getLongestName(b);
        return (a.length>bName.length)?a:bName
    },treeName)??treeName
    return longestChildName
}
function getLongestNameArray(trees:Tree[]):string{
    return trees.map(tree=>getLongestName(tree)).reduce((nameA,nameB)=>nameA.length>nameB.length?nameA:nameB,"");
}
console.log(getLongestNameArray(treeArray))
Sign up to request clarification or add additional context in comments.

Comments

1

As you are using recursive structure in your interface, Therefore you have to use recursive function to solve your problem.

interface Tree {
    id: string
    name: string
    child?: Tree[]
}

const treeArray: Tree[] = [
    {
        id: "test", name: "test1", child: [
            { id: "Test#2", name: "@2ndtest" },
            {
                id: "test#2", name: "2ndtest", child: [
                    { id: "Test#2#3", name: "@3rdtestTestTest" },
                ]
            }
        ]
    },
    { id: "testtest", name: "testtest2" }
]
const getLongestNameObjectInTreeArray = (arr: Tree[])  => {
    let longestlength = 0
    let output: Tree;
    arr.forEach(e => {
        if (e.name.length > longestlength) {
            longestlength = e.name.length;
            output = { id: e.id, name: e.name };
        }

        if (e.child) {
            const childOutput = getLongestNameObjectInTreeArray(e.child);
            if (childOutput.name.length > longestlength) {
                longestlength = childOutput.name.length;
                output = { id: childOutput.id, name: childOutput.name };
            }
        }
    });

    return output;
}

console.log(getLongestNameObjectInTreeArray(treeArray))

1 Comment

Awesome!! I really really appreciate your answer!! I don't have any ideas to use recursive function. I clearly understand!! Thank you so much!!

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.