Junior — Senior
Get a list of object names by type "Devices"
livecode
Task condition
The task consists of two parts:
- Output a string containing the object names.
- Implement a function that forms a string with the object names (separated by commas) if their
object_typebelongs to types where theclassfield is equal to the string "Devices".
interface IObjectItem {
id: number;
name: string;
object_type: number;
}
interface IObjectType {
id: number;
class: string;
}
const objects: IObjectItem[] = [
{ id: 1, name: "Test 1", object_type: 1 },
{ id: 2, name: "Test 2", object_type: 1 },
{ id: 3, name: "Test 3", object_type: 2 },
{ id: 4, name: "Test 4", object_type: 3 },
{ id: 5, name: "Test 5", object_type: 4 },
];
const object_types: IObjectType[] = [
{ id: 1, class: "Devices" },
{ id: 2, class: "Devices" },
{ id: 3, class: "Ports" },
{ id: 4, class: "Cables" },
];
function getNames(objects: IObjectItem[], object_types: IObjectType[]): string {
const mapIds: number[] = [];
object_types.forEach((type) => {
if (type.class === "Devices") mapIds.push(type.id);
});
const result: string[] = [];
objects.forEach((obj) => {
if (mapIds.includes(obj.object_type)) result.push(obj.name);
});
return result.join(", ");
}
console.log(getNames(objects, object_types));