I want to bifurcate currencyId
and currencyCode
from the rest of the items in the control
array.
Right now it is giving output like :
[{
channels: {ANDROID: 0,EMAIL: 0,INBOX: 0,IOS: 0,SMS: 0,TELEGRAM: 0,currencyCode: "CFA",currencyId: 11},
currency: {currencyCode: "CFA",currencyId: 11}
}]
But I want output like :
[{
channels: {ANDROID: 0,EMAIL: 0,INBOX: 0,IOS: 0,SMS: 0,TELEGRAM: 0},
currency: {currencyCode: "CFA",currencyId: 11}
}]
This is my code so far:
let chargesData = [
{
"SMS": 0,
"IOS": 0,
"ANDROID": 0,
"EMAIL": 0,
"currencyId": 11,
"currencyCode": "CFA",
"TELEGRAM": 0,
"INBOX": 0
},
{
"SMS": 10,
"IOS": 10,
"ANDROID": 10,
"EMAIL": 10,
"currencyId": 12,
"currencyCode": "IND",
"TELEGRAM": 10,
"INBOX": 10
}
]
let control = [];
let currency;
for(let charges of chargesData) {
let chargesChannels = Object.keys(charges)
let currencyCode = chargesChannels.indexOf("currencyCode");
let currencyId = chargesChannels.indexOf("currencyId");
chargesChannels.splice(currencyCode, 1);
chargesChannels.splice(currencyId, 1);
currency = { currencyCode: charges.currencyCode, currencyId: charges.currencyId };
control.push({
channels: charges,
currency: currency
})
}
console.log(control);
How to do this?
2
Answers
you can iterate over array and then over object properties using
in
operator and then separate out the object as needed.You can do this using
.map
anddestructuring
: