简体   繁体   English

尝试组合两个对象键 ID 匹配的数据集

[英]Trying to combine two datasets where object key ids match

I have two datasets and have been trying to combine them, but have no idea where to start.我有两个数据集,并一直在尝试将它们组合起来,但不知道从哪里开始。 I am making two API calls and below is a small example of a response:我正在进行两个 API 调用,下面是一个响应的小例子:

{
    early_game_items: {
        57: 16
        59: 60
        106: 1
        180: 54
        214: 28
        232: 6
    },
    late_game_items: {
        108: 1
        116: 1
    },
    mid_game_items: {
        1: 52
        102: 3
        108: 4
        116: 1
        193: 1
        194: 1
        223: 1
        232: 73
    }
}

The other data set is numbered from 1 - 300ish and is an object made of other objects.另一个数据集编号为 1 - 300ish,是由其他对象组成的对象。 Below is an snippet:下面是一个片段:

const items = [{
            "id": 57,
            "name": "void_stone",
            "cost": 825,
            "secret_shop": 1,
            "side_shop": 0,
            "recipe": 0,
            "localized_name": "Void Stone"
        },
        {
            "id": 58,
            "name": "mystic_staff",
            "cost": 2700,
            "secret_shop": 1,
            "side_shop": 0,
            "recipe": 0,
            "localized_name": "Mystic Staff"
        },
        {
            "id": 59,
            "name": "energy_booster",
            "cost": 900,
            "secret_shop": 1,
            "side_shop": 0,
            "recipe": 0,
            "localized_name": "Energy Booster"
        }...]

I need to put the data from the second data set into the first by matching the key in the first data set with the id in the second.我需要通过将第一个数据集中的键与第二个数据集中的 id 匹配,将第二个数据集中的数据放入第一个数据集中。 For example:例如:

{
    early_game_items: {
        57:  {amount: 16, name: 'void_stone', cost: 825}
        59:  {amount: 60...
        106: {amount: 1...
        180: {amount: 54...
        214: {amount: 28...
        232: {amount: 6...
    }... 

Thank you so much for looking this over!非常感谢您查看此内容! I am new to js and am really trying to learn.我是 js 新手,我真的很想学习。

Naming the datasets dataset1 & dataset2, and assuming dataset2 is an array of objects:将数据集命名为 dataset1 和 dataset2,并假设 dataset2 是一个对象数组:

let result = {};

// key = 'early_game_items', 'mid_game_items', 'late_game_items'
for(let key in dataset1) {
    const itemGroup = dataset1[key];
    let _itemGroup = {}; 

    for(let id in itemGroup) {
        let _item = { amount: itemGroup[id] };
        // find item by id, in second dataset
        const item = dataset2.find(i => i.id == id) || {};
        // get name & cost via destructuring
        const { name, cost } = item;
        
        _item.name = name;
        _item.cost = cost;
        // store in new itemGroup
        _itemGroup[id] = _item;
    }
    // store in result
    result[key] = _itemGroup
}

console.log(result);

If dataset2 is an object with numbers as keys, you'll need to modify the "find by id" function:如果 dataset2 是一个以数字为键的对象,则需要修改“按 id 查找”函数:

// find item by id, in second dataset
const item = Object.values(dataset2).find(i => i.id === id) || {};

Such as another answer naming the datasets dataset1 & dataset2, and assuming dataset2 is an array of objects.例如命名数据集 dataset1 和 dataset2 的另一个答案,并假设 dataset2 是一个对象数组。 If dataset2 is big array, this answer has better performance:如果 dataset2 是大数组,则此答案具有更好的性能:

let result = {};
// key = 'early_game_items', 'mid_game_items', 'late_game_items'
for(let key in dataset1) {
    result[key] = {}; 
    for(let id in dataset1[key]) {
        result[key][id] = { amount: dataset1[key][id] };
    }
}
for(let id in dataset2) {
   for(let key in dataset1) {
      let _item;
      if(_item=result[key][id]){
         const { name, cost } = dataset2[id];
         _item.name = name;
         _item.cost = cost;
      }
   }
}
console.log(result);

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

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