skip to Main Content

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


  1. you can iterate over array and then over object properties using in operator and then separate out the object as needed.

    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
        }
    ];
    
    const controls = [];
    chargesData.forEach(charges=>{
        const control={
            channels:{},
            currency:{}
        }
        for (const key in charges) {
            if(["currencyCode","currencyId"].includes(key)){
                control.currency[key]=charges[key];
            }else{
                control.channels[key]=charges[key];
            }
        }
        controls.push(control);
    });
    console.log(controls);
    Login or Signup to reply.
  2. You can do this using .map and destructuring:

    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
      }
    ]
    
    const arr = chargesData.map(e => {
      const {currencyCode, currencyId, ...channels} = e;
      return {
        channels: channels,
        currency: {currencyCode, currencyId}
      }
    });
    
    console.log(arr);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search