【问题标题】:Process the Object and store in efficient way以有效的方式处理和存储对象
【发布时间】:2020-12-26 02:35:04
【问题描述】:

我收到一个具有基本键值模式的对象(Object1),然后我想在MongoDB中处理并保存输出,使其包含数组对象对于本质上是动态的“位置”和“地址”。

请建议我任何有效的方法来做到这一点。

const Object1 = { 
                        date:'07-Sep-2020',

                        Address_1_Ftime: 2,
                        Address_1_Rtime: 0,
                        Address_1_Stime: 49,

                        Address_2_Ftime: 12,
                        Address_2_Rtime: 40,
                        Address_2_Stime: 9

                        Location1Name:'SAV2',Location1Val:30,
                        Location2Name:'LT232',Location2Val:130,
                     }


const Output = 
{
    date : '07-Sep-2020',
    Address:
    [
        {
            name:'Address_1',
            Ftime:2,
            Rtime:0,
            Stime:49
        },
        {
            name:'Address_2',
            Ftime:12,
            Rtime:40,
            Stime:9
        }
    ],
    Locations:[
    {
        text:Location1,
        name:SAV2,
        value:30
    },
    {
        text:Location1,
        name:LT232,
        value:130
    }
    ]
}

【问题讨论】:

  • 我使用的是 SQL 服务器,但我跳到了 MongoDB,因此非常感谢任何建议/将其保存在结构中。
  • 当您的尝试没有按预期工作时,您应该向我们展示您尝试过的内容以及其他人的帮助。 SO 不是免费的代码编写服务
  • 好的,我会试试这个。因为这是我的第一个问题,所以错过了专业的提问。您能否提供任何社区/参考链接以供我自己学习和做。
  • 看看使用 Object.keys() 并解析各种键名
  • 非常感谢@Thomas 给我指导,我完全迷失了方向,花了很长时间才完成。

标签: javascript node.js arrays mongodb object


【解决方案1】:

尝试完成后,请检查以下代码。 如果您没有关注那里的内容,请告诉我。

const Object1 = { 
  date:'07-Sep-2020',

  Address_1_Ftime: 2,
  Address_1_Rtime: 0,
  Address_1_Stime: 49,

  Address_2_Ftime: 12,
  Address_2_Rtime: 40,
  Address_2_Stime: 9,

  Location1Name:'SAV2',Location1Val:30,
  Location2Name:'LT232',Location2Val:130,
};

// Initialize the output
const Output = {
  Address: {},
  Locations: {}
};

Object.keys(Object1).forEach(key => {
  const value = Object1[key];
  if (key === 'date') {
    Output.date = value;
  } else if (key.startsWith('Address')) {
    const addressName = key.substr(0, key.length - 6);
    const addressFieldKey = key.substr(key.length - 5);
    Output.Address[addressName] = Output.Address[addressName] || {};
    Output.Address[addressName][addressFieldKey] = value;
  } else if (key.startsWith('Location')) {
    const fieldKeyLength = key.endsWith('Name') ? 4 : 3;
    const locationName = key.substr(0, key.length - fieldKeyLength);
    const locationFieldKey = key.substr(key.length - fieldKeyLength);
    Output.Locations[locationName] = Output.Locations[locationName] || {};
    Output.Locations[locationName][locationFieldKey] = value;
  }
});

// At this point, Address and Locations of Output are still objects not array. So you need the following change.

Output.Address = Object.keys(Output.Address).map(key => ({
  name: key,
  ...Output.Address[key]
}));

Output.Locations = Object.keys(Output.Locations).map(key => ({
  text: key,
  name: Output.Locations[key].Name,
  value: Output.Locations[key].Val
}));

console.log(Output);

【讨论】:

  • 非常感谢,您的回答对我这样的新手非常有用。你真好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多