首頁  >  問答  >  主體

在數組中取得指定的元素

<p>我得到了<strong>[Document<Recording<string, any>>, number][]</strong>的輸出 每個元素的描述如下:</p> <pre class="brush:php;toolbar:false;">[ Document{ page: '我曾在ABC公司工作', meta: { id: emloyee-111, name: 'John"} }, 245 ] [ Document{ page: '我是軟體開發人員', meta: { id: emloyee-444, name: 'Marry"} }, 789 ] 對於 (employee of employees) { // 取得id // 取得name // 取得數字 245, 789 }</pre> <p>在typescript(或javasSript)中,如何取得兩個員工的數字(245, 789)以及他們的id和name。 </p>
P粉191323236P粉191323236430 天前539

全部回覆(1)我來回復

  • P粉268654873

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

    只需遍歷輸出

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

    或者,如果你想從一個員工陣列中取得資料:

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

    回覆
    0
  • 取消回覆