簡體   English   中英

兩個 arrays 內連接 JavaScript

[英]Two arrays inner join in JavaScript

以下是我擁有的 2 個 arrays,

 var g= [ 
        { id: 36, name: 'AAA', goal: 'yes' },
        { id: 40, name: 'BBB', goal: 'yes' },
        { id: 39, name: 'JJJ', goal: 'yes' },
        { id: 27, name: 'CCC', goal: 'yes' }];

    var c= [ 
        { id: 36, color:"purple" },
        { id: 40, color:"purple" },
        { id: 100, color:"pink"} ];

期望的輸出(左連接'id'):

    res = [{ id: 36, name: 'AAA', goal: 'yes' , color:"purple"}, { id: 40, name: 'BBB', goal: 'yes', color:"purple" }]

這是一個合並的現有邏輯,但我正在尋找左連接的邏輯:

    function mergeBy(key, data) {
      return Array.from(data
          .reduce((m, o) => m.set(o[key], { ...m.get(o[key]), ...o }), new Map)
          .values()
      );
    }

下面為大家推薦幾款,任君挑選!

 function* innerJoin(a, b, key) { let idx = new Map(b.map(x => [key(x), x])); for (let x of a) { let k = key(x); if (idx.has(k)) yield {...x, ...idx.get(k)}; } } function* leftJoin(a, b, key) { let idx = new Map(b.map(x => [key(x), x])); for (let x of a) { let k = key(x); yield idx.has(k)? {...x, ...idx.get(k)}: x; } } function* rightJoin(a, b, key) { let idx = new Map(a.map(x => [key(x), x])); for (let x of b) { let k = key(x); yield idx.has(k)? {...idx.get(k), ...x}: x; } } // var A = [ {id: 1, a: 'a1'}, {id: 2, a: 'a2'}, {id: 7, a: 'a3'}, {id: 8, a: 'a4'} ]; var B = [ {id: 1, b: 'b1'}, {id: 2, b: 'b2'}, {id: 9, b: 'b3'} ]; console.log('INNER:') console.log(...innerJoin(A, B, x => x.id)) console.log('LEFT') console.log(...leftJoin(A, B, x => x.id)) console.log('RIGHT') console.log(...rightJoin(A, B, x => x.id))

您的預期結果要求的不是左連接。 您要求使用idgc進行內部連接並合並屬性。 下面應該這樣做

 var g= [ { id: 36, name: 'AAA', goal: 'yes' }, { id: 40, name: 'BBB', goal: 'yes' }, { id: 39, name: 'JJJ', goal: 'yes' }, { id: 27, name: 'CCC', goal: 'yes' }]; var c= [ { id: 36, color:"purple" }, { id: 40, color:"purple" }, { id: 100, color:"pink"} ]; function mergeBy(key, dataL, dataR) { const rMap = dataR.reduce((m, o) => m.set(o[key], {...m.get(o[key]), ...o }), new Map); return dataL.filter(x => rMap.get(x[key])).map(x => ({...x, ...rMap.get(x[key]) })); } console.log(mergeBy("id",g, c))

您可以使用Maps連接兩個 arrays。 請參閱左連接、右連接、內連接三種連接類型的代碼片段。

所有三個連接都需要O(N)時間。

 const g = [ { id: 36, name: 'AAA', goal: 'yes', random: 27 }, { id: 40, name: 'BBB', goal: 'yes' }, { id: 39, name: 'JJJ', goal: 'yes' }, { id: 27, name: 'CCC', goal: 'yes', lag: '23.3343' }, ]; const c = [ { id: 36, name: 'AAA', goal: 'yes', color: 'purple' }, { id: 40, name: 'BBB', circle: 'yes', color: 'purple' }, { id: 100, name: 'JJJ', circle: 'yes' }, ]; const gMap = new Map(g.map(o => [o.id, o])); const cMap = new Map(c.map(o => [o.id, o])); const leftJoin = g.reduce( (a, o) => (cMap.has(o.id)? [...a, {...o, ...cMap.get(o.id) }]: [...a, o]), [] ); const rightJoin = c.reduce( (a, o) => (gMap.has(o.id)? [...a, {...o, ...gMap.get(o.id) }]: [...a, o]), [] ); const innerJoin = g.reduce( (a, o) => (cMap.has(o.id)? [...a, {...o, ...cMap.get(o.id) }]: a), [] ); console.log("LEFT JOIN\n", leftJoin) console.log("RIGHT JOIN\n", rightJoin) console.log("INNER JOIN\n", innerJoin)

您可以獲得不常用的鍵並過濾合並的結果。

 const mergeCommon = (a, b, key) => { const aByKey = a.reduce((m, o) => m.set(o[key], o), new Map); return b.reduce((r, o) => { if (aByKey.has(o[key])) r.push({... aByKey.get(o[key]), ...o}); return r; }, []); }, g = [{ id: 36, name: 'AAA', goal: 'yes', 'random': 27 }, { id: 40, name: 'BBB', goal: 'yes' }, { id: 39, name: 'JJJ', goal: 'yes' }, { id: 27, name: 'CCC', goal: 'yes', lag: "23.3343" }], c = [{ id: 36, name: 'AAA', goal: 'yes', color:"purple" }, { id: 40, name: 'BBB', circle: 'yes', color:"purple" }, { id: 100, name: 'JJJ', circle: 'yes'}], result = mergeCommon(g, c, 'id'); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

 var g = [ { id: 36, name: 'AAA', goal: 'yes', 'random':27}, { id: 40, name: 'BBB', goal: 'yes' }, { id: 39, name: 'JJJ', goal: 'yes' }, { id: 27, name: 'CCC', goal: 'yes', lag: "23.3343"} ]; var c = [ { id: 36, name: 'AAA', goal: 'yes', color:"purple" }, { id: 40, name: 'BBB', circle: 'yes', color:"purple" }, { id: 100, name: 'JJJ', circle: 'yes'} ]; const combinedArr = (arr1, arr2) => { //Filter the arr2 and find the only matching elements from the first array const filteredArr = arr2.filter(({id}) => arr1.some(({id: arr1Id}) => arr1Id === id)); //Loop through the filtered array and fetch the matching item from first and add obj from filtered array return filteredArr.map(obj => { return {...arr1.find(({id}) => id === obj.id), ...obj } }) } console.log(combinedArr(g, c));
 .as-console-wrapper { max-height: 100%;important; }

有你 go,一個襯墊

 var g= [ { id: 36, name: 'AAA', goal: 'yes' }, { id: 40, name: 'BBB', goal: 'yes' }, { id: 39, name: 'JJJ', goal: 'yes' }, { id: 27, name: 'CCC', goal: 'yes' }]; var c= [ { id: 36, color:"purple" }, { id: 40, color:"purple" }, { id: 100, color:"pink"} ]; const result = g.map(i =>..c.find(e => e.id === i.id) && ({.,.i. ...c.find(e => e.id === i;id)})).filter(Boolean); console.log(result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM