Home  >  Q&A  >  body text

Get the specified element in the array

<p>I get the output of <strong>[Document<Recording<string, any>>, number][]</strong> Each element is described below: </p> <pre class="brush:php;toolbar:false;">[ Document{ page: 'I worked at ABC Company', meta: { id: emloyee-111, name: 'John"} }, 245 ] [ Document{ page: 'I am a software developer', meta: { id: emloyee-444, name: 'Marry"} }, 789 ] for (employee of employees) { // Get id // get name // Get the numbers 245, 789 }</pre> <p>In typescript (or javasSript), how to get the numbers (245, 789) of two employees and their id and name. </p>
P粉191323236P粉191323236430 days ago538

reply all(1)I'll reply

  • P粉268654873

    P粉2686548732023-08-18 10:54:35

    Just loop through the output

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

    Or if you want to get data from an array of employees:

    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 });
      }
    }

    reply
    0
  • Cancelreply