Home > OS >  how to create new array based on condition in key , value pair
how to create new array based on condition in key , value pair

Time:12-13

I have a array like this and I want a desired output based on username and number of services booked by user. I want data such as number of quantity of services related to username

[
    {
        
        "customerNo": 1001,
        "username": "user3",
        "serviceName": "Tempo",
        "quantity": 1,
    },
    {
       ,
        "customerNo": 1002,
        "username": "user4",
        "serviceName": "Social",
        "quantity": 1,
       
    },
    {
        "customerNo": 1001,
        "username": "user3",
        "serviceName": "Mango",
        "quantity": 1,
    },
    {
        "customerNo": 1001,
        "username": "user3",
        "serviceName": "Mango",
        "quantity": 1,
    },
    {
        "customerNo": 1003,
        "username": "user5",
        "serviceName": "Social",
        "quantity": 1,
    },
    {
        "customerNo": 1002,
        "username": "user4",
        "serviceName": "Social",
        "quantity": 1,
    }
]

Desired output:

[
    {
        
        "customerNo": 1001,
        "username": "user3",
        "serviceName": "Tempo",
        "quantity": 1,
    },
    {
        
        "customerNo": 1001,
        "username": "user3",
        "serviceName": "Mango",
        "quantity": 2,
    },
    {
       
        "customerNo": 1002,
        "username": "user4",
        "serviceName": "Mango",
        "quantity": 2,
       
    },
    {
        "customerNo": 1003,
        "username": "user5",
        "serviceName": "Social",
        "quantity": 1,
    }
]

Thanks in advance.

I did tried for forEach and reduce but could not get desired result may be I am doing something wrong.

CodePudding user response:

Both forEach and reduce are not the right methods to go with. As Pointy, mentioned, you should include your code so others can get an exact idea of what you're doing. It sounds like you may be looking for the .filter() method. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

so may look like something like this

const filteredArray = unfliteredArray.filter(element => element.userName === "user3")

I suggest you don't hardcode your username, but this is just an example.

CodePudding user response:

You need to:

  1. Sort based on customerNo
  2. Remove duplicates with added quantity (?)

There you go:

const userLogs = [{
    "customerNo": 1001,
    "username": "user3",
    "serviceName": "Tempo",
    "quantity": 1,
  },
  {
    "customerNo": 1002,
    "username": "user4",
    "serviceName": "Social",
    "quantity": 1,

  },
  {
    "customerNo": 1001,
    "username": "user3",
    "serviceName": "Mango",
    "quantity": 1,
  },
  {
    "customerNo": 1001,
    "username": "user3",
    "serviceName": "Mango",
    "quantity": 1,
  },
  {
    "customerNo": 1003,
    "username": "user5",
    "serviceName": "Social",
    "quantity": 1,
  },
  {
    "customerNo": 1002,
    "username": "user4",
    "serviceName": "Social",
    "quantity": 1,
  }
]

// Sort based on customerNo
userLogs.sort((a, b) => a.customerNo - b.customerNo);

// console.log(userLogs)

let aggregatedValues = {};

let aggregatedArray = userLogs.map((item) => {
  // Check if an already exists
  if (aggregatedValues[item.customerNo   item.serviceName]) {
    // If it does, increment the quantity
    aggregatedValues[item.customerNo   item.serviceName].quantity  = item.quantity;
  } else {
    // If it doesn't, add a new object
    aggregatedValues[item.customerNo   item.serviceName] = {
      customerNo: item.customerNo,
      username: item.username,
      serviceName: item.serviceName,
      quantity: item.quantity,
    };
  }
});

let finalArray = Object.values(aggregatedValues).reduce((acc, item) => {
  acc.push(item);
  return acc;
}, []);

console.log(finalArray)

Edit: Corrected code suggestion by @3limin4t0r

  • Related