Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

group all items with same name js

const inputArray = [ 
    { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
    { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
    { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
    { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
    { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
    { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
    { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
    { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
];

var outObject = inputArray.reduce(function(a, e) {
  // GROUP BY estimated key (estKey), well, may be a just plain key
  // a -- Accumulator result object
  // e -- sequentally checked Element, the Element that is tested just at this itaration

  // new grouping name may be calculated, but must be based on real value of real field
  let estKey = (e['Phase']); 

  (a[estKey] ? a[estKey] : (a[estKey] = null || [])).push(e);
  return a;
}, {});

console.log(outObject);
Comment

PREVIOUS NEXT
Code Example
Javascript :: array.unshift in javascript 
Javascript :: set _id to id 
Javascript :: javascript async function 
Javascript :: js tolowercase 
Javascript :: string to json js 
Javascript :: discord.js MessageEmbed 
Javascript :: todashcase javascript 
Javascript :: useNavigate history back 
Javascript :: file extension name in js 
Javascript :: javascript dynamic arrays 
Javascript :: javascript convert image to base64 
Javascript :: tinymce update textarea value using jquery 
Javascript :: tolocalestring javascript currency fixing 2 decimal places 
Javascript :: react read multiple files with filereader 
Javascript :: make form submit on new tab using jquery 
Javascript :: change key name in array of objects javascript 
Javascript :: date regex format 
Javascript :: how to make a div appear onclick 
Javascript :: javascript tofixed no trailing zeros 
Javascript :: change select value jquery 
Javascript :: countdown timer javascript stack overflow 
Javascript :: jquery div onload function 
Javascript :: mongodb push to index 
Javascript :: how to delete a letter from a string in javascript 
Javascript :: node filesystem change directory of a file 
Javascript :: typescript express next middleware type 
Javascript :: javascript string normalize method 
Javascript :: object flatten js 
Javascript :: how to submit form on changed url in function in jquery 
Javascript :: babylon js camera position 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =