简体   繁体   English

如何通过 javascript 中的键“内部连接”两个 arrays?

[英]How to "inner join" two arrays by a key in javascript?

I have two arrays containing objects like so:我有两个 arrays 包含这样的对象:

const array1 = 
[
{
    "id": 1,
    "mass": 149,
    "height": 180,
    "dob": "2003-09-04"
},
{
    "id": 2,
    "mass": 140,
    "height": 175,
    "dob": "2000-02-12",
},
{
    "id": 3,
    "mass": 143,
    "height": 170,
    "dob": "2001-11-04" 
}
]

const array2 = 
[
{
    "id": 1,
    "name": "James",
    "sport": "Football"
},
{
    "id": 2,
    "name": "Adam",
    "sport": "Tennis"
}
]

I would like to merge them such that a combined array only contains data where the id appears in both array1 and array2.我想合并它们,使组合数组仅包含id出现在 array1 和 array2 中的数据。

So far I'm using lodash _.merge which returns:到目前为止,我使用的是 lodash _.merge ,它返回:

const merged = _.merge(array1, array2)

merged = [
{
"id": 1,
"mass": 149,
"height": 180,
"dob": "2003-09-04",
"name": "James",
"sport": "Football"
},
{
"id": 2,
"mass": 140,
"height": 175,
"dob": "2000-02-12",
"name": "Adam",
"sport": "Tennis"
},
{
"id": 3,
"mass": 143,
"height": 170,
"dob": "2001-11-04" ,
}
]

I would just like merged to contain data where id appears in both, ie:我只想merged以包含id出现在两者中的数据,即:

[{
"id": 1,
"mass": 149,
"height": 180,
"dob": "2003-09-04",
"name": "James",
"sport": "Football"
},
{
"id": 2,
"mass": 140,
"height": 175,
"dob": "2000-02-12",
"name": "Adam",
"sport": "Tennis"
}]

I believe the equivalent function in something like SQL would be an "Inner join"我相信 SQL 之类的等效 function 将是“内部连接”

Any help would be greatly appreciated!任何帮助将不胜感激!

You can filter your first array with Array.filter to only include objects that are present in your second array before performing the same merge.您可以使用 Array.filter 过滤您的第一个数组,以便在执行相同的合并之前仅包含第二个数组中存在的对象。

let merged = array1.filter(e => array2.some(f => f.id == e.id));

 const array1 = [ { "id": 1, "mass": 149, "height": 180, "dob": 2003-09-04 }, { "id": 2, "mass": 140, "height": 175, "dob": 2000-02-12, }, { "id": 3, "mass": 143, "height": 170, "dob": 2001-11-04 } ] const array2 = [ { "id": 1, "name": "James", "sport": "Football" }, { "id": 2, "name": "Adam", "sport": "Tennis" } ] let merged = array1.filter(e => array2.some(f => f.id == e.id)); merged = _.merge(merged, array2); console.log(merged);
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

You need to map them from one to the other.您需要将它们从一个到另一个 map。 Basic idea using reduce where we track the item by the id.使用 reduce 的基本思想,我们通过 id 跟踪项目。

 const array1 = [{ "id": 1, "mass": 149, "height": 180, "dob": '2003-09-04' }, { "id": 2, "mass": 140, "height": 175, "dob": '2000-02-12', }, { "id": 3, "mass": 143, "height": 170, "dob": '2001-11-04' } ] const array2 = [{ "id": 1, "name": "James", "sport": "Football" }, { "id": 2, "name": "Adam", "sport": "Tennis" } ] const result = Object.values([...array1, ...array2].reduce((acc, userData) => { // have you seen the id before? If yes, use it, if no use and empty object const data = acc[userData.id] || {}; //merge the objects together and store the updated data by the id acc[userData.id] = {...data, ...userData }; return acc }, {})); console.log(result);

Now that will include everything.现在这将包括所有内容。 If you only want the items that are in both, you can do it in two loops.如果您只想要两者中的项目,则可以在两个循环中进行。

 const array1 = [{ "id": 1, "mass": 149, "height": 180, "dob": '2003-09-04' }, { "id": 2, "mass": 140, "height": 175, "dob": '2000-02-12', }, { "id": 3, "mass": 143, "height": 170, "dob": '2001-11-04' } ] const array2 = [{ "id": 1, "name": "James", "sport": "Football" }, { "id": 2, "name": "Adam", "sport": "Tennis" } ] // create a look up by the id const array1MappedById = array1.reduce((acc, userData) => { acc[userData.id] = userData; return acc }, {}); // Loop over the second array and make a new array with the merged data const result = array2.reduce((arr, userData) => { const array1Data = array1MappedById[userData.id]; if (array1Data) { arr.push({...array1Data, ...userData}); } return arr; }, []); console.log(result);

this will merge the data where the id is the same id这将合并 id 相同的数据

const merged = array2.reduce((arr, current) => {
    let array1Data = {...array1}[current.id];
    if (array1Data) {
      arr.push({...array1Data, ...current});
    }
    return arr;
}, []);

console.log(merged);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM