【问题标题】:Array of objects: delete keys from object and add them as a new object to the same array对象数组:从对象中删除键并将它们作为新对象添加到同一数组中
【发布时间】:2021-05-12 00:57:55
【问题描述】:

我有一个如下所示的对象数组:

const products = [{
    "listPrice": 50,
    "discount": 10,
    "total": 45,
    "users": "",
    "userNumber": 10,
    "userlistPrice": 120,
    "userDiscount": 10,
    "userTotal": 108
  },
  {
    "listPrice": 1000,
    "discount": 10,
    "total": 900,
    "userNumber": 100,
    "userlistPrice": 1200,
    "userDiscount": 0,
    "userTotal": 1200
  },
  {
    "listPrice": 100,
    "discount": 0,
    "total": 100,
    "userNumber": "",
    "userlistPrice": "",
    "userDiscount": 0,
    "userTotal": ""
  },
  {
    "listPrice": 100,
    "discount": 10,
    "total": 90,
    "userNumber": 100,
    "userlistPrice": 1200,
    "userDiscount": 0,
    "userTotal": 1200
  },
  {
    "listPrice": 5000,
    "discount": 0,
    "total": 5000,
    "userNumber": "",
    "userlistPrice": "",
    "userDiscount": 0,
    "userTotal": ""
  },
  {
    "name": "",
    "listPrice": "",
    "discount": 0,
    "total": ""
  },
  {
    "name": "",
    "listPrice": "",
    "discount": 0,
    "total": ""
  }
]

我需要将所有users 键(userNumberuserListPrice 等)放入一个新对象之后,紧跟在同一个数组中。

首先我尝试使用for loop 进行此操作,然后尝试使用forEach,最后使用filter,但我没有得到任何结果。我也尝试使用splice,但无法正确获取索引。

如果有人能指出我正确的方向,我将不胜感激。

预期结果是这样的:

const products = [{
    "listPrice": 50,
    "discount": 10,
    "total": 45,
  },
{
    "users": "",
    "userNumber": 10,
    "userlistPrice": 120,
    "userDiscount": 10,
    "userTotal": 108

},
  {
    "listPrice": 1000,
    "discount": 10,
    "total": 900,
  },
{
    "userNumber": 100,
    "userlistPrice": 1200,
    "userDiscount": 0,
    "userTotal": 1200
}]

解决这个问题的尝试在这个小提琴中:https://jsfiddle.net/7hkouzpn/

【问题讨论】:

  • “我尝试使用 for 循环,然后尝试使用 forEach ...” 请附上您的尝试,以便我们帮助您理解和解决问题
  • 使用for 循环。向我们展示您的尝试
  • 这里有一个提示:你可以使用.map((ele) => return ....)
  • 预期输出是什么?你能帮我解决这个问题,我可以帮你搞定逻辑吗?
  • 这是我试过的小提琴:jsfiddle.net/7hkouzpn

标签: javascript arrays


【解决方案1】:

您可以编写一个“分区”函数,将对象分成两部分(非“用户”和“用户”道具)和flatMap 使用此函数的数组:

result = products.flatMap(obj => {
    let parts = [{}, {}]
    for (let key in obj)
        parts[key.startsWith('user') ? 1 : 0][key] = obj[key]
    return parts
})

如果您不想要此代码可能创建的空对象,最简单的选择是事后将它们过滤掉:

result = result.filter(obj => Object.keys(obj).length > 0)

【讨论】:

  • 我真的很喜欢这个超级简洁的解决方案:)
  • 很好的解决方案!我在您的解决方案中添加了一个空对象过滤器:filter(value => Object.keys(value).length !== 0),以防没有用户密钥。
  • @GeorgeGrigorita:对,已将其添加到帖子中。
【解决方案2】:

如果有人能指出我正确的方向,我将不胜感激。

这是一个很好的方向,您可以使用forEach()

我希望它会有所帮助。

const products = [{
    "listPrice": 50,
    "discount": 10,
    "total": 45,
    "users": "",
    "userNumber": 10,
    "userlistPrice": 120,
    "userDiscount": 10,
    "userTotal": 108
  },
  {
    "listPrice": 1000,
    "discount": 10,
    "total": 900,
    "userNumber": 100,
    "userlistPrice": 1200,
    "userDiscount": 0,
    "userTotal": 1200
  },
  {
    "listPrice": 100,
    "discount": 0,
    "total": 100,
    "userNumber": "",
    "userlistPrice": "",
    "userDiscount": 0,
    "userTotal": ""
  },
  {
    "listPrice": 100,
    "discount": 10,
    "total": 90,
    "userNumber": 100,
    "userlistPrice": 1200,
    "userDiscount": 0,
    "userTotal": 1200
  },
  {
    "listPrice": 5000,
    "discount": 0,
    "total": 5000,
    "userNumber": "",
    "userlistPrice": "",
    "userDiscount": 0,
    "userTotal": ""
  },
  {
    "name": "",
    "listPrice": "",
    "discount": 0,
    "total": ""
  },
  {
    "name": "",
    "listPrice": "",
    "discount": 0,
    "total": ""
  }
]

var newArray = [] 
var newObject = {}
var newObjectKeys = {}

products.forEach(product => {
   newObject = { "name": product.name }
   newObjectKeys = {"objKey": Object.keys(product) }
  newArray.push(newObject)
});

console.log(newArray)
console.log(newObjectKeys)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 2022-01-16
    • 2022-12-28
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    相关资源
    最近更新 更多