Forming an array by grouping using the properties from source array of objects in a particular order

javascript group by multiple properties
javascript group array of objects by property
group array of objects by key php
array reduce
lodash group by
group array of objects by key c#
javascript combine array of objects based on property value
angular 6 group by array

I have an array of objects with the following structure that is being sent as response:

    let sampleData = [
      { values: { High: 4, Low: 5,  Medium: 7 } , time: "1571372233234" , sum: 16 },
      { values: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9},
      { time: "14354545454", sum: 0},
      { time: "14354545454", sum: 0} }
    ];


I need to take each key within each object inside array and form an array out of it. Basically grouping based on the key present in all the objects.If the object has no "values" it should return 0 across the val1,val2,val3.

But the issue , the order of the keys "high,low,medium" inside "values" object in the question is unpredictable. With whatever code I have written keys that are present in the first object will be grouped and follows that order. But I want be it any input order, It should always give me this order "High Medium Low" Order.

The resultant array of objects should look like and should always follow "High Medium Order":

result = [
  { name: 'High', data: [4, 5, 0, 0] }, 
  { name: 'Medium', data: [5, 3, 0, 0] }, 
  { name: 'Low', data: [7, 1, 0, 0] }
]

I have tried the following:

let sampleData = [{ values: { High: 4, Low: 5,  Medium: 7 } , time: "1571372233234" , sum: 16 },{ values: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9},{ time: "14354545454", sum: 0},{ time: "14354545454", sum: 0 }];
    
const keySet = new Set(sampleData.flatMap(a => Object.keys(a.values || {})));

const merged = sampleData.reduce((acc, {
  values = {}
}) => {
  keySet.forEach(name => {
    acc[name] = acc[name] || {
      name,
      data: []
    };
    acc[name].data.push(values[name] || 0);
  });
  return acc;
}, {});

console.log(merged);

You could take the result set and iterate for the key and array for the value.

let sampleData = [{ values: { High: 4, Low: 5,  Medium: 7 }, time: "1571372233234", sum: 16 }, { values: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234", sum: 9 }, { time: "14354545454", sum: 0 }, { time: "14354545454", sum: 0 }],
    keys = ['High', 'Low', 'Medium'],
    grouped = sampleData.reduce((r, { values = {} } = {}) => {
        r.forEach(({ name, data }) => data.push(values[name] || 0));
        return r;
    }, keys.map(name => ({ name, data: [] })));

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to group an array of objects by key, var grouped = _. In plain Javascript, you could use Array#reduce with an object . var cars Removing the property you are grouping by from the objects should be trivial if required: var cars The short version to group an array of objects by a certain key in es6: Notice that the source array change his elements! I have an array of objects with the following structure that is being sent as response: let sampleData = [ { values: { High: 4, Low: 5, Medium: 7 } , time


You can check for the undefined value when the attr values is not present.

sampleData.reduce((a, {values: {High = 0, Low = 0, Medium = 0} = {}})
                                                               ^
                                                               |
                                                               +-- When attr "values" is 
                                                                   undefined initialize
                                                                   as an "empty" object.

let sampleData = [{ values: { High: 4, Low: 5,  Medium: 7 } , time: "1571372233234" , sum: 16 },{ values: { High: 5, Low: 3, Medium : 1 }, time: "1571372233234" , sum: 9},{ time: "14354545454", sum: 0},{ time: "14354545454", sum: 0}];
    
let result = Object.values(sampleData.reduce((a, {values: {High = 0, Low = 0, Medium = 0} = {}}) => {
  (a["High"] || (a["High"] = {name: "High", data: []})).data.push(High);
  (a["Low"] || (a["Low"] = {name: "Low", data: []})).data.push(Low);
  (a["Medium"] || (a["Medium"] = {name: "Medium", data: []})).data.push(Medium);

  return a;
}, Object.create(null)));
    
 console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Javascript groupby array of objects, The example below shows providing a default group order. Group Array of JavaScript Objects by Key or Property Value - group-objects-by-property. before diving into creating the groupBy function using array reduce. a single indexed string column. first(array, [callback], [thisArg]) source npm package� In this article, I will explain about the groupBy array of object in javascript.. “JavaScript group an array of objects by key” is published by Edison Devadoss.


You are returning an object and as such the order of the keys is not guaranteed. You probably want an array instead of an object.

You could just construct a final merged object like this

mergedFinal = [ merged['High'], merged['Medium'], merged['Low'] ]

How to group an array of objects through a key using Array reduce , reduce before diving into creating the groupBy function using array reduce. How reduce works. In plain english, it reduce the array into single� The reduce() function is great and it has several uses like sum all the values of an array or in an object array, counting for particular items in the array, grouping objects, merging arrays contained in array of objects, removing duplicates, etc.


Array methods, The arrays are objects, so we can try to use delete : Starting from the index 1 it removed 1 element. isConcatSpreadable property, then it's treated as an array by concat : its elements How do we find an object with the specific condition? The method arr.reverse reverses the order of elements in arr . There are some really cool use cases for .reduce() outlined in the MDN docs that provide examples on how to do things likes flattening an array of arrays, grouping objects by a property, and removing duplicate items in array..forEach() Applies a function on each item in an array. Example. Log each array item to the console


Grouping Data, This is a bit different then many group_by concepts, where only a single level of nesting And that has been converted to a nice array of objects via our data reading a key property - which is what we used as the grouping value using the key function. The order of the nest.key calls determines the order of the grouping. For example, in PHP it's possible to group an associative array by some key, so you will be able to display it's data by some order (group). In this article, we'll share with you a tiny snippet that allows you to group items of an array by some key.


jQuery.map(), Array-like objects — those with a .length property and a value on the .length - 1 index — must be converted to actual arrays before being passed to $.map() . Group-Object returns a table with one row for each property value and a column that displays the number of items with that value. If you specify more than one property, Group-Object first groups them by the values of the first property, and then, within each property group, it groups by the value of the next property.