Home > Blockchain >  How to modify an array and combine values in array by user
How to modify an array and combine values in array by user

Time:04-15

I have an array list of upload_count by date and by users. Need to update format and want final array by email_id and with upload_count array by date_value, and if no upload_count for that date then add 0 (zero).

var list = [
{
    "date_value": "2022-04-01",
    "first_name": "user1",
    "email_id": "[email protected]",
    "upload_count": 1
},
{
    "date_value": "2022-04-01",
    "first_name": "user2",
    "email_id": "[email protected]",
    "upload_count": 8
},
{
    "date_value": "2022-04-05",
    "first_name": "user2",
    "email_id": "[email protected]",
    "upload_count": 1
},
{
    "date_value": "2022-04-06",
    "first_name": "user2",
    "email_id": "[email protected]",
    "upload_count": 2
}]

I have tried to updated list by group array function by passing key.

// Function to group object
function groupArrayByKey(list, key) {
  return list.reduce(function(val, x) {
    (val[x[key]] = val[x[key]] || []).push(x);
    return val;
  }, {});
};

var expArr= groupArrayByKey(list, "email_id");

Not get proper output.

Expected output like below: For date: 2022-04-01 to 2022-04-06

 [
    {
        "first_name": "user1",
        "email_id": "[email protected]",
        "upload_count": [1,0,0,0,0,0] 
    },
    {
        "first_name": "user2",
        "email_id": "[email protected]",
        "upload_count": [8,0,0,0,1,2] 
    }
]

CodePudding user response:

function collate_update_count(inputs, collation_key, start_date, end_date) {
  const collated = {};
  if (inputs && inputs.length) {
    inputs.forEach((input) => {
      const collation_value = `${input[collation_key]}`;
      if (collated[collation_value]) {
        collated[collation_value].upload_count.push([input.date_value, input.upload_count || 0]);
      } else {
        collated[collation_value] = {
          first_name: input.first_name,
          email_id: input.email_id,
          upload_count: [
            [input.date_value, input.upload_count || 0]
          ]
        };
      }
    });
  }
  return Object.values(collated).map(collation => {
    return Object.assign(Object.assign({}, collation), {
      ["upload_count"]: get_upload_count_of(collation.upload_count)
    });
  });

  function get_upload_count_of(datewise_upload_counts) {
    let current_date = new Date(start_date);
    const upload_counts = [];
    while (current_date <= new Date(end_date)) {
      upload_counts.push((datewise_upload_counts
        .find(datewise_count => current_date.getTime() === new Date(datewise_count[0]).getTime()) || [0, 0])[1]);
      current_date.setDate(current_date.getDate()   1);
    }
    return upload_counts;
  }
}

Illustration

"use strict";

function collate_update_count(inputs, collation_key, start_date, end_date) {
  const collated = {};
  if (inputs && inputs.length) {
    inputs.forEach((input) => {
      const collation_value = `${input[collation_key]}`;
      if (collated[collation_value]) {
        collated[collation_value].upload_count.push([input.date_value, input.upload_count || 0]);
      } else {
        collated[collation_value] = {
          first_name: input.first_name,
          email_id: input.email_id,
          upload_count: [
            [input.date_value, input.upload_count || 0]
          ]
        };
      }
    });
  }
  return Object.values(collated).map(collation => {
    return Object.assign(Object.assign({}, collation), {
      ["upload_count"]: get_upload_count_of(collation.upload_count)
    });
  });

  function get_upload_count_of(datewise_upload_counts) {
    let current_date = new Date(start_date);
    const upload_counts = [];
    while (current_date <= new Date(end_date)) {
      upload_counts.push((datewise_upload_counts
        .find(datewise_count => current_date.getTime() === new Date(datewise_count[0]).getTime()) || [0, 0])[1]);
      current_date.setDate(current_date.getDate()   1);
    }
    return upload_counts;
  }
}
const list = [{
    "date_value": "2022-04-01",
    "first_name": "user1",
    "email_id": "[email protected]",
    "upload_count": 1
  },
  {
    "date_value": "2022-04-01",
    "first_name": "user2",
    "email_id": "[email protected]",
    "upload_count": 8
  },
  {
    "date_value": "2022-04-05",
    "first_name": "user2",
    "email_id": "[email protected]",
    "upload_count": 1
  },
  {
    "date_value": "2022-04-06",
    "first_name": "user2",
    "email_id": "[email protected]",
    "upload_count": 2
  }
];
console.log('Using email_id:\n', collate_update_count(list, "email_id", "2022-04-01", "2022-04-06"));

console.log('Using first_name:\n', collate_update_count(list, "first_name", "2022-04-01", "2022-04-05"));

  • Related