JSFiddle - React, Tailwind, and code Playground

by Evgeniy Kvasyuk

HTML

<div>
<p>
Please write function that get object as an argument and function-handler that will be applyed for this object itself, every value of every property of this object and all properties values in depth. 
Also shoud work if value is an array 
</p>
</div>

TypeScript

function func(obj:unknown, fieldFn: (field: unknown) => void) {

		fieldFn(obj);

    if (Array.isArray(obj)) {
        return obj.map(item => func(item, fieldFn));
    }


    const result = {};
    Object.keys(obj).forEach(key => {
        result[key] = func(obj[key], fieldFn);
    });
    return result;
}

const obj = {
    a: 1,
    b: {
        c: 2,
        d: [3, 4]
    },
    e: [5, { f: 6 }]
};

const fieldFn = (value) => console.log(value);

console.log(func(obj, fieldFn))