skip to Main Content

I got have the output of [Document<Recording<string, any>>, number][]
each element is described as:

 [
    Document{
      page: 'I have worked for ABC company',
      meta: { id: emloyee-111, name: 'John"}
    },
    245
 ]
 [
    Document{
      page: 'I'm Software developer',
      meta: { id: emloyee-444, name: 'Marry"}
    },
    789
  ]

  For (employee of employees) {
     // get id
     // get name
     // get the number 245, 789
  }

In typescript (or javasSript), how to I get the numbers (245, 789) of two employees and their id and name.

2

Answers


  1. Just iterate over the output

    for (const [doc, num] of output) {
      const { id, name } = doc.employee;
      console.log({ id, name, num });
    }
    

    Or, if you’re trying to get to the data from an employees array:

    for (const employee of employee) {
      const entry = output.find(([doc]) => doc.employee.id === employee.id);
    
      if (entry) {
        const [doc, num] = entry;
        const { id, name } = employee;
        console.log({ id, name, num });
      }
    }
    
    Login or Signup to reply.
  2. You should supply your types and provide actual TypeScript code.

    The following structure should work:

    interface Recording {
      id: string;
      name: string;
    }
    
    interface EmployeeDocument {
      page: string;
      meta: Recording;
    }
    
    type Employee = [EmployeeDocument, number];
    
    const employees: Employee[] = [
      [
        {
          page: "I have worked for ABC company",
          meta: { id: "emloyee-111", name: "John" },
        },
        245,
      ],
      [
        {
          page: "I'm Software developer",
          meta: { id: "emloyee-444", name: "Marry" },
        },
        789,
      ],
    ];
    
    for (let employee of employees) {
      const [{ page, meta: { id, name } }, count ] = employee;
      console.log({ page, id, name, count });
    }
    
    "use strict";
    
    const employees = [
        [
            {
                page: "I have worked for ABC company",
                meta: { id: "emloyee-111", name: "John" },
            },
            245,
        ],
        [
            {
                page: "I'm Software developer",
                meta: { id: "emloyee-444", name: "Marry" },
            },
            789,
        ],
    ];
    
    for (let employee of employees) {
        const [{ page, meta: { id, name } }, count] = employee;
        console.log({ page, id, name, count });
    }
    .as-console-wrapper { top: 0; max-height: 100% !important; }

    Output

    {
      "page": "I have worked for ABC company",
      "id": "emloyee-111",
      "name": "John",
      "count": 245
    }
    {
      "page": "I'm Software developer",
      "id": "emloyee-444",
      "name": "Marry",
      "count": 789
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search