简体   繁体   中英

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

I have an array of objects that looks like this:

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": ""
  }
]

I need to get all the users keys ( userNumber , userListPrice and so on) into a new object right after the one it was in, in the same array.

First I tried to do it with a for loop then a tried with a forEach and finally with filter but I didn't get anywhere. I also tried to use splice but I couldn't get the index right.

If someone could point me in the right direction, I would very much appreciate.

Expected result is this:

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
}]

Attempts so solve this are in this fiddle: https://jsfiddle.net/7hkouzpn/

You can write a "partition" function that divides an object into two parts (non-"user" and "user" props) and flatMap your array with this function:

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

If you don't want empty objects that might be created by this code, the simplest option would be to filter them out afterwards:

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

If someone could point me in the right direction, I would very much appreciate.

This aa good direction you could take using forEach() .

I hoped it'll help.

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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