【问题标题】:Remove null object and can be written in more efficient way删除空对象,可以用更有效的方式编写
【发布时间】:2021-11-09 11:27:30
【问题描述】:

我有一个用户对象,我正在尝试使用用户对象的值创建不同的对象,称为元素。我的代码如下所示。我想知道是否可以以更有效的方式编写此代码,因为我想从输出中删除空对象。

JSFIDDLE

var users = {
  "accounts": [{
      "accountId": "210001"
    },
    {
      "accountId": "90000",
      "accountText": "Sample"
    },
    {
      "accountId": "4618891",
      "accountText": "Test"
    }
  ]
};
var obj = {};
var element = {},
  cart = [];
users["accounts"].forEach(user => {
  obj = {
    ...obj,
    "M": {
      "accountId": {
        "S": user.accountId
      },
      "accountText": {
        "S": user.accountText
      }
    }
  }
  cart.push(obj);
})
element["L"] = cart;
console.log(JSON.stringify(element))

预期输出

{
    "L": [{
            "M": {
                "accountId": {
                    "S": "210001"
                }
            }
        },
        {
            "M": {
                "accountId": {
                    "S": "90000"
                },
                "accountText": {
                    "S": "Sample"
                }
            }
        },
        {
            "M": {
                "accountId": {
                    "S": "4618891"
                },
                "accountText": {
                    "S": "Test"
                }
            }
        }
    ]
}

【问题讨论】:

    标签: javascript node.js arrays json


    【解决方案1】:

    不是更小但更易读

    var users = {
      "accounts": [{
          "accountId": "210001"
        },
        {
          "accountId": "90000",
          "accountText": "Sample"
        },
        {
          "accountId": "4618891",
          "accountText": "Test"
        }
      ]
    };
    var obj = {};
    var element = {},
      cart = [];
    users["accounts"].forEach(user => {
      obj = { ...obj, "M": { } };
      if (user.accountText) obj.M.accountText = { "S": user.accountText}
      if (user.accountId)   obj.M.accountId   = { "S": user.accountId }
      cart.push(obj);
    })
    element["L"] = cart;
    console.log(element)

    通过删除减少

    var users = {
      "accounts": [{
          "accountId": "210001"
        },
        {
          "accountId": "90000",
          "accountText": "Sample"
        },
        {
          "accountId": "4618891",
          "accountText": "Test"
        }
      ]
    };
    const element = {},
      cart = users["accounts"].reduce((acc,user) => {
      const obj = {
        "M": { 
          accountText: { "S": user.accountText}, 
          accountId : { "S": user.accountId }
        }
      }
      if (!user.accountText) delete obj.M.accountText
      if (!user.accountId) delete obj.M.accountId
      acc.push(obj);
      return acc
    },[])
    element["L"] = cart; 
    console.log(element)

    【讨论】:

      【解决方案2】:

      也许是带有动态键的更短代码?

      const users = {
        accounts: [{
            accountId: '210001'
          },
          {
            accountId: '90000',
            accountText: 'Sample'
          },
          {
            accountId: '4618891',
            accountText: 'Test'
          },
          {
            accountId: '46188911',
            accountText: null
          }
        ]
      }
      
      const aa = users.accounts.map(x => {
        return {
          M: Object.entries(x)
            .reduce((acc, curr) => {
              return curr[1] // check for null values
                ? { ...acc, [curr[0]]: { S: curr[1] } }
                : acc
            }, {})
        }
      })
      
      const elements = { L: aa }
      
      console.log(JSON.stringify(elements, null, 2))

      对于动态内外键:

      const users = {
        accounts: [{
          accountId: '210001'
        },
        {
          accountId: '90000',
          accountText: 'Sample'
        },
        {
          accountId: '4618891',
          accountText: 'Test'
        },
        {
          accountId: '46188911',
          accountText: null
        }
        ]
      }
      
      const outerKey = 'MM'
      const innerKey = 'SS'
      
      const aa = users.accounts.map(x => {
        return {
          [outerKey]: Object.entries(x)
            .reduce((acc, curr) => {
              return curr[1] // check for null values
                ? { ...acc, [curr[0]]: { [innerKey]: curr[1] } }
                : acc
            }, {})
        }
      })
      
      const elements = { L: aa }
      
      console.log(JSON.stringify(elements, null, 2))

      【讨论】:

      • 如何保持 M 和 S(作为键)也是动态的?
      猜你喜欢
      • 1970-01-01
      • 2015-02-12
      • 2020-05-17
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 2022-06-15
      • 2020-02-24
      • 1970-01-01
      相关资源
      最近更新 更多