Sobes.tech
Junior — Senior

Unification of array element structure

livecode

Task condition

From the server, an array of objects data arrives, each with its own unique field schema. It is required to implement a function flatData that takes the data array and returns a new array where all elements are brought to the same structure. In the resulting array, each object should contain only the properties id and name, where name is taken from the original object regardless of whether it was nested inside another object or was at the top level.

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: any) => data;

// [
//   { id: 1, name: "siberia can code 📘" },
//   { id: 2, name: "siberia can code 📘" },
//   { id: 3, name: "siberia can code 📘" },
// ];