Sobes.tech
Junior — Senior

Converting an array of objects to a unified format

livecode

Task condition

It is necessary to implement a function that takes an array of objects and returns a new array where each element is converted to a unified structure. An example dataset is used below:

 const data = [
    { id: 1, name: "siberia can code 💬" },
    { id: 2, body: { name: "siberia can code 💬" } },
    { id: 3, type: "person", name: "siberia", lastname: "can code 💬" }
];

const flatData = (data) => data.map(item => {
    return {
        id: item.id,
        name: item.name || item.body?.name || `${item.name} ${item.lastname}`
    };
});

// Expected result:
// [
//     { id: 1, name: "siberia can code 💬" },
//     { id: 2, name: "siberia can code 💬" },
//     { id: 3, name: "siberia can code 💬" }
// ]