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/
forloop. Show us what you tried.map((ele) => return ....)