Skip to content Skip to sidebar Skip to footer

Create Multidimensional Array Of [key,value] With Key Unique Count As Value From Array Of JSON Object

Currently i have array of json object returned by server data: [ { billed: 'No', designation: 'ASE', involvement: 'Full Time', name: 'Rishi Ranabhat', project

Solution 1:

Iterate over the data with reduce to create an object grouped by type. Here's a reusable function - just pass in the data and the type.

const data = [{"billed":"No","designation":"ASE","involvement":"Full Time","name":"Rishi Ranabhat","project":"ABC"},{"billed":"No","designation":"ASE","involvement":"Full Time","name":"Biplap Bhattarai","project":"DEF"},{"billed":"No","designation":"SE","involvement":"Part Time","name":"Ram k","project":"DEF"}];

function getCount(data, type) {

  // `map` out the data by type
  const typeArr = data.map((obj) => obj[type]);

  // Iterate over the type data. We pass in an initial
  // object to capture the counts, so we need to use
  // `Object.values` to grab the object values at the end
  // of the iteration
  return Object.values(typeArr.reduce((acc, id) => {

    // If the key doesn't exist in the accumulator object
    // create it and create a new array at its value
    acc[id] = acc[id] || [id, 0];

    // Increment the second index (the count)
    acc[id][1]++;

    // Return the object for the next iteration
    return acc;
  }, {}));
}

console.log(getCount(data, 'designation'));
console.log(getCount(data, 'project'));

Further reading

Alternatively, if you wanted to do this in one operation and return an object containing the grouped information, you could use another reduce to iterate over the main data keys:

const data = [{"billed":"No","designation":"ASE","involvement":"Full Time","name":"Rishi Ranabhat","project":"ABC"},{"billed":"No","designation":"ASE","involvement":"Full Time","name":"Biplap Bhattarai","project":"DEF"},{"billed":"No","designation":"SE","involvement":"Part Time","name":"Ram k","project":"DEF"}];

function getCounts(data) {

  // Grab the data keys. It assumes that each object in
  // the array has the same keys
  const keys = Object.keys(data[0]);

  // Using `reduce` iterate over the keys to build
  // up an object that groups the results from the inner
  // `reduce` operation by key
  return keys.reduce((out, key) => {

    // `map` out the data by type
    const typeArr = data.map((obj) => obj[key]);

    // Iterate over the type data. We pass in an initial
    // object to capture the counts, so we need to use
    // `Object.values` to grab the object values at the end
    // of the iteration
    out[key] = Object.values(typeArr.reduce((acc, id) => {

      // If the key doesn't exist in the accumulator object
      // create it and create a new array at its value
      acc[id] = acc[id] || [id, 0];

      // Increment the second index (the count)
      acc[id][1]++;

      // Return the object for the next iteration
      return acc;
    }, {}));
    
    // Return the `out` object for the next iteration
    return out;

  }, {});

}

console.log(getCounts(data));

Solution 2:

Lot's of ways to do this. Here is a simple way (could be cleaned up, but just trying to demo):

View on JSFiddle

const data = [{
    billed: "No",
    designation: "ASE",
    involvement: "Full Time",
    name: "Rishi Ranabhat",
    project: "ABC"
  },
  {
    billed: "No",
    designation: "ASE",
    involvement: "Full Time",
    name: "Biplap Bhattarai",
    project: "DEF"
  },
  {
    billed: "No",
    designation: "SE",
    involvement: "Part Time",
    name: "Ram k",
    project: "DEF"
  }
];

const designations = [],
      projects = [];

for (const record of data) {
  // Count designations
  if (!designations[record.designation]) {
    designations[record.designation] = 0;
  }
  
  designations[record.designation] = designations[record.designation] + 1;

  // Count projects
  if (!projects[record.project]) {
    projects[record.project] = 0;
  }
  projects[record.project] = projects[record.project] + 1;
}

// Merge sets
const final = [designations, projects];

console.log(final);

Solution 3:

const data = [
  {
    billed: "No",
    designation: "ASE",
    involvement: "Full Time",
    name: "Rishi Ranabhat",
    project: "ABC"
  },
  {
    billed: "No",
    designation: "ASE",
    involvement: "Full Time",
    name: "Biplap Bhattarai",
    project: "DEF"
  },
  {
    billed: "No",
    designation: "SE",
    involvement: "Part Time",
    name: "Ram k",
    project: "DEF"
  }
];

const result = data.reduce((acc,cur) => {
  for(let k in cur) {
    if(!acc[k]) {
      acc[k] = [[cur[k], 1]];
    } else {
      const idx = acc[k].findIndex(e => e[0] === cur[k]);
      if(idx !== -1) {
        acc[k][idx][1]++
      } else {
        acc[k].push([cur[k], 1])
      }
    }
  }
  return acc;
}, {});

console.log(result)

Post a Comment for "Create Multidimensional Array Of [key,value] With Key Unique Count As Value From Array Of JSON Object"