0

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/

5
  • 3
    "I tried to do it with a for loop then a tried with a forEach ..." Please include your attempt(s) so we can help you understand and solve the problem Commented May 11, 2021 at 8:22
  • Use for loop. Show us what you tried Commented May 11, 2021 at 8:22
  • Here's a hint : You can use .map((ele) => return ....) Commented May 11, 2021 at 8:26
  • what is the expected output ? can you help me with that so I can help you with the logic? Commented May 11, 2021 at 8:26
  • This is the fiddle I tried in: jsfiddle.net/7hkouzpn Commented May 11, 2021 at 8:36

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

3 Comments

I really like this super concise solution :)
Great solution! What I've added to your solution was a filter for empty objects: filter(value => Object.keys(value).length !== 0) in case there are no user keys.
@GeorgeGrigorita: right, added that to the post.
0

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

This a a 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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.