主頁 >  其他 > 合并兩個具有共享值的陣列

合并兩個具有共享值的陣列

2022-09-15 19:10:31 其他

我有兩個物件陣列:

let employees = [
  { name: 'Jason', job_id: '101' },
  { name: 'Sarah', job_id: '102' },
  { name: 'Jack', job_id: '102' }
]

let jobs = [
  { job_id: '101', position: 'Designer' },
  { job_id: '102', position: 'Developer' }
]

我是否可以使用 vanilla javascript 將這些陣列合并為一個,如下所示:

let employees = [
  { name: 'Jason', job_id: [job_id: '101', position: 'Designer'] },
  { name: 'Sarah', job_id: [job_id: '102', position: 'Developer'] },
  { name: 'Jack', job_id: [job_id: '102', position: 'Developer'] }
]

以下是我現在所擁有的。它確實給了我正確的結果,但是如果可能的話,我寧愿不使用嵌套回圈。

employees.forEach(employee => {
  for (let index = 0; index < jobs.length; index  ) {
    if (employee.job_id == jobs[index].job_id) {
      employee.job_id= jobs[index];
    }
  }
})

uj5u.com熱心網友回復:

任務不是很復雜,所以我將嘗試通過現場演示做出簡單的回答。

這里的想法是遍歷employees陣列,然后構造一個具有所需結構的物件,然后將其推入最終陣列。

最終陣列上的每個物件都將具有以下屬性:

  • name:等于employees陣列上的名稱(每次迭代)
  • job:為了讓事情更清楚,我會使用job而不是job_id(你在你想要的結果中使用它)。job是一個物件, 包含
    • id:job_id來自employees陣列
    • positionjobs:陣列中的相應位置。

為了得到上述結果,我們將使用reduce構建最終陣列的方法。

另一個任務是根據employees 表positionjobs陣列中獲取相應的 ,。job_id為此,我們將使用find回傳在jobs陣列中找到的第一個匹配項的方法。

話雖如此,讓我們來一個現場演示(演示有一些有用的評論):

const employees = [{
      name: 'Jason',
      job_id: '101'
    },
    {
      name: 'Sarah',
      job_id: '102'
    },
    {
      name: 'Jack',
      job_id: '102'
    }
  ],
  jobs = [{
      job_id: '101',
      position: 'Designer'
    },
    {
      job_id: '102',
      position: 'Developer'
    }
  ],
  /** 
   * "res" is the resulting array after performing the needed operations 
   * "reduce" method loops through the "employees" array and populates the "res" array along the way
   */
  res = employees.reduce((a, c) => {
    /**
     * a: the accumulator, it holds the last value from the previous iteration. Initially it equals to an empty array "[]" (see 3rd argument passed to the "reduce" method)
     * c: is the current element from the "employees" array on each iteration
     */

    /** adds an object that follows the intended structure to the final array */
    a.push({
      name: c.name,
      job: {
        id: c.job_id,
        /** 
         * find the respective "position" based on the "job_id" of the current element of element of the "employees" arrray 
         * el: holds the element from "jobs" array on each iteration of the "find" method
         * the condition is once we find an elemnt having "job_id" equals to the current element (i mean "c" parameter) "job_id" then return that object's "position" attribute
         * once we found the "position" we simply save it on the object we're constructing
         */
        position: jobs.find(el => el.job_id == c.job_id).position
      }
    });
    /** return the array so that the next iteration of "reduce" can do its work */
    return a;
  }, []);

/** print the result */
console.log(res);

上面的演示絕對不是完成事情的唯一方法,當然有一些奇特的ES6方法可以做到這一點,尤其是在構造物件時。為了簡單起見,我不會使用那些花哨/復雜的技巧來使事情盡可能簡單易懂。

reduce在 MDN 上了解有關該方法的更多資訊。

find在 MDN 上了解有關該方法的更多資訊。

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/508338.html

標籤:javascript 数组

上一篇:選擇一個間隔為7天的日期后如何獲取下一個日期

下一篇:返回列表

標籤雲
其他(144758) Python(37227) JavaScript(24835) Java(16400) C(14945) 區塊鏈(8236) C#(7950) AI(7469) 爪哇(7387) html(6766) MySQL(6705) 基礎類(6313) sql(6080) 熊猫(6051) PHP(5775) 数组(5733) R(5304) Linux(5174) 反应(5164) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4408) 数据框(4307) css(4246) 节点.js(4012) C語言(3288) json(3236) C++語言(3117) 列表(3116) 扑(3071) 安卓(2989) 打字稿(2944) VBA(2784) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2378) ASP.NET(2364) MongoDB(2315) 麻木的(2284) 正则表达式(2221) 字典(2211) 循环(2196) 擅长(2159) 迅速(2157) 镖(2146) 功能(1966) Web開發(1951) python-3.x(1912) 弹簧靴(1904) xml(1865) for循环(1841) 谷歌表格(1836) Unity3D(1822) PostgreSQL(1805) 網絡通信(1793) .NETCore(1787) .NET技术(1786) 蟒蛇-3.x(1774)

熱門瀏覽
  • 網閘典型架構簡述

    網閘架構一般分為兩種:三主機的三系統架構網閘和雙主機的2+1架構網閘。 三主機架構分別為內端機、外端機和仲裁機。三機無論從軟體和硬體上均各自獨立。首先從硬體上來看,三機都用各自獨立的主板、記憶體及存盤設備。從軟體上來看,三機有各自獨立的作業系統。這樣能達到完全的三機獨立。對于“2+1”系統,“2”分為 ......

    uj5u.com 2020-09-10 02:00:44 more
  • 如何從xshell上傳檔案到centos linux虛擬機里

    如何從xshell上傳檔案到centos linux虛擬機里及:虛擬機CentOs下執行 yum -y install lrzsz命令,出現錯誤:鏡像無法找到軟體包 前言 一、安裝lrzsz步驟 二、上傳檔案 三、遇到的問題及解決方案 總結 前言 提示:其實很簡單,往虛擬機上安裝一個上傳檔案的工具 ......

    uj5u.com 2020-09-10 02:00:47 more
  • 一、SQLMAP入門

    一、SQLMAP入門 1、判斷是否存在注入 sqlmap.py -u 網址/id=1 id=1不可缺少。當注入點后面的引數大于兩個時。需要加雙引號, sqlmap.py -u "網址/id=1&uid=1" 2、判斷文本中的請求是否存在注入 從文本中加載http請求,SQLMAP可以從一個文本檔案中 ......

    uj5u.com 2020-09-10 02:00:50 more
  • Metasploit 簡單使用教程

    metasploit 簡單使用教程 浩先生, 2020-08-28 16:18:25 分類專欄: kail 網路安全 linux 文章標簽: linux資訊安全 編輯 著作權 metasploit 使用教程 前言 一、Metasploit是什么? 二、準備作業 三、具體步驟 前言 Msfconsole ......

    uj5u.com 2020-09-10 02:00:53 more
  • 游戲逆向之驅動層與用戶層通訊

    驅動層代碼: #pragma once #include <ntifs.h> #define add_code CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ANY_ACCESS) /* 更多游戲逆向視頻www.yxfzedu.com ......

    uj5u.com 2020-09-10 02:00:56 more
  • 北斗電力時鐘(北斗授時服務器)讓網路資料更精準

    北斗電力時鐘(北斗授時服務器)讓網路資料更精準 北斗電力時鐘(北斗授時服務器)讓網路資料更精準 京準電子科技官微——ahjzsz 近幾年,資訊技術的得了快速發展,互聯網在逐漸普及,其在人們生活和生產中都得到了廣泛應用,并且取得了不錯的應用效果。計算機網路資訊在電力系統中的應用,一方面使電力系統的運行 ......

    uj5u.com 2020-09-10 02:01:03 more
  • 【CTF】CTFHub 技能樹 彩蛋 writeup

    ?碎碎念 CTFHub:https://www.ctfhub.com/ 筆者入門CTF時時剛開始刷的是bugku的舊平臺,后來才有了CTFHub。 感覺不論是網頁UI設計,還是題目質量,賽事跟蹤,工具軟體都做得很不錯。 而且因為獨到的金幣制度的確讓人有一種想去刷題賺金幣的感覺。 個人還是非常喜歡這個 ......

    uj5u.com 2020-09-10 02:04:05 more
  • 02windows基礎操作

    我學到了一下幾點 Windows系統目錄結構與滲透的作用 常見Windows的服務詳解 Windows埠詳解 常用的Windows注冊表詳解 hacker DOS命令詳解(net user / type /md /rd/ dir /cd /net use copy、批處理 等) 利用dos命令制作 ......

    uj5u.com 2020-09-10 02:04:18 more
  • 03.Linux基礎操作

    我學到了以下幾點 01Linux系統介紹02系統安裝,密碼啊破解03Linux常用命令04LAMP 01LINUX windows: win03 8 12 16 19 配置不繁瑣 Linux:redhat,centos(紅帽社區版),Ubuntu server,suse unix:金融機構,證券,銀 ......

    uj5u.com 2020-09-10 02:04:30 more
  • 05HTML

    01HTML介紹 02頭部標簽講解03基礎標簽講解04表單標簽講解 HTML前段語言 js1.了解代碼2.根據代碼 懂得挖掘漏洞 (POST注入/XSS漏洞上傳)3.黑帽seo 白帽seo 客戶網站被黑帽植入劫持代碼如何處理4.熟悉html表單 <html><head><title>TDK標題,描述 ......

    uj5u.com 2020-09-10 02:04:36 more
最新发布