簡體   English   中英

對象數組:從 object 中刪除鍵並將它們作為新的 object 添加到同一數組中

[英]Array of objects: delete keys from object and add them as a new object to the same array

我有一個看起來像這樣的對象數組:

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等)放入一個新的 object 之后,在同一個數組中。

首先,我嘗試使用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/

您可以編寫一個“分區” function 將 object 分成兩部分(非“用戶”和“用戶”道具)並使用此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)

如果有人能指出我正確的方向,我將不勝感激。

這是您可以使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM