Group values of an object in a new array - JavaScript

javascript group by multiple properties
group array of objects by key javascript
lodash group by
javascript combine array of objects based on property value
javascript array group by sum
javascript array group by count
json group by key javascript
javascript group by property

I need to return an array with multiple elements pulled from an array of objects.

This is de original object:

    data: Array(11)
       0: {id: "1", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-01", PD_PETROLEO: 369.168, PD_GAS: 96, …}
       1: {id: "2", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-02", PD_PETROLEO: 364.21, PD_GAS: 96, …}
       2: {id: "3", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-03", PD_PETROLEO: 361.559, PD_GAS: 96, …}
       3: {id: "4", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-04", PD_PETROLEO: 322.346, PD_GAS: 81, …}
       4: {id: "5", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-05", PD_PETROLEO: 339.027, PD_GAS: 44, …}
       5: {id: "6", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-06", PD_PETROLEO: 346.939, PD_GAS: 44, …}
       6: {id: "7", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-08", PD_PETROLEO: 321.934, PD_GAS: 44, …}
       7: {id: "8", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-09", PD_PETROLEO: 351.404, PD_GAS: 44, …}
       8: {id: "9", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 360.167, PD_GAS: 44, …}
       9: {id: "10", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 371.895, PD_GAS: 44, …}
      10: {id: "11", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-11", PD_PETROLEO: 356.232, PD_GAS: 66, …}

I'm using the map function like this:

    const FECHA_DATA = [
       datosDiarios &&
       datosDiarios.data.map(fecha => {
          const { FECHA, PD_GAS, PD_AGUA, PD_PETROLEO } = fecha;
          return FECHA;
       }),
    ];

And I'm getting this result:

    [Array(11)]
       0: (11) ["2010-06-01", "2010-06-02", "2010-06-03", "2010-06-04", "2010-06-05", "2010-06-06", "2010-06-08", "2010-06-09", "2010-06-1..."]

But, I need the array to be like this for PD_GAS, PD_PETROLEO, and PD_AGUA:

    [Array(11)]
       0: (11) ["2010-06-01", "2010-06-02", "2010-06-03", "2010-06-04", "2010-06-05", "2010-06-06", "2010-06-08", "2010-06-09", "2010-06-1..."],
       1: (11) [96, 96, 96, 81, 44, 44, 44, 44, 44, 44, 66],
       2: (11) [1138.48, 1138.48, 1138.48, 1146.85, 1160.95, 1160.95, 1160.95, 1160.95, 1160.95, 1160.95, 1157.58],
       3: (11) [369.168, 364.21, 361.559, 322.346, 339.027, 346.939, 321.934, 351.404, 360.167, 371.895, 356.232],

I think that with the function map it is possible but if someone has another way to do this, I'll appreciate.

instead of map you need to use reduce



const data = [
        {id: "1", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-01", PD_PETROLEO: 369.168, PD_GAS: 96}, 
       {id: "2", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-02", PD_PETROLEO: 364.21, PD_GAS: 96},
       {id: "3", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-03", PD_PETROLEO: 361.559, PD_GAS: 96},
       {id: "4", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-04", PD_PETROLEO: 322.346, PD_GAS: 81},
       {id: "5", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-05", PD_PETROLEO: 339.027, PD_GAS: 44},
       {id: "6", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-06", PD_PETROLEO: 346.939, PD_GAS: 44},
       {id: "7", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-08", PD_PETROLEO: 321.934, PD_GAS: 44},
       {id: "8", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-09", PD_PETROLEO: 351.404, PD_GAS: 44},
       {id: "9", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 360.167, PD_GAS: 44},
       {id: "10", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 371.895, PD_GAS: 44},
      {id: "11", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-11", PD_PETROLEO: 356.232, PD_GAS: 66}];

const result = data.reduce((acc,item) => {
    const { FECHA, PD_GAS, PD_AGUA, PD_PETROLEO } = item;
    acc[0].push(FECHA);
    acc[1].push(PD_GAS);
    acc[2].push(PD_AGUA);
    acc[3].push(PD_PETROLEO );

    return acc;
}, [[],[],[],[]]);

console.log(result);

How to group an array of objects by key, In plain Javascript, you could use Array#reduce with an object just at this itaration // new grouping name may be calculated, but must be based on real value of� Let’s say e have a parentArray that contains many sub arrays each of the same size, each sub array is an array of objects containing two properties: key and value. Within a subarray it is confirmed that two objects cannot have the same key but all subarrays have the same pair of n keys where n is the size of the sub array.

The reason .map() is not working is because mapping an array produces a 1->1 result from your original dataset. It looks like you want a 4->1 result, where for every record in the original data, you want to produce an entry in 4 arrays. The easiest way to do this would probably be with the .reduce() function instead, like so:

let data = [{id: "1", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-01", PD_PETROLEO: 369.168, PD_GAS: 96},
{id: "2", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-02", PD_PETROLEO: 364.21, PD_GAS: 96},
{id: "3", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-03", PD_PETROLEO: 361.559, PD_GAS: 96},
{id: "4", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-04", PD_PETROLEO: 322.346, PD_GAS: 81},
{id: "5", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-05", PD_PETROLEO: 339.027, PD_GAS: 44},
{id: "6", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-06", PD_PETROLEO: 346.939, PD_GAS: 44},
{id: "7", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-08", PD_PETROLEO: 321.934, PD_GAS: 44},
{id: "8", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-09", PD_PETROLEO: 351.404, PD_GAS: 44},
{id: "9", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 360.167, PD_GAS: 44},
{id: "10", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 371.895, PD_GAS: 44},
{id: "11", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-11", PD_PETROLEO: 356.232, PD_GAS: 66}];

let mappedData = data.reduce((res, curr) => {
  // push whatever property you want to each spot in the array
  // you can change the order however you'd like
  res[0].push(curr.COMPLETAMIENTO);
  res[1].push(curr.FECHA);
  res[2].push(curr.PD_PETROLEO);
  res[3].push(curr.PD_GAS);
  return res;
}, [[], [], [], []]); // start with an array of 4 empty arrays into which we can push our values

console.log(mappedData)

JavaScript group an array of objects by key, The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value. let group =� Either flatten the objects first, like { brand: 'Audi', color_value: 'black' } or pass a function taking each object in the array, returning the desired value on that object. Eg. instead of const value = obj[key] do const value = keyFn(obj). Another approach would be to pass a key with dots, like 'color.value' and have the function parse that.

Array#map does not work here, because you need to rotate the matrix.

You could take an array of keys in the wanted order and push to the same index as the keys.

var data = [{ id: "1", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-01", PD_PETROLEO: 369.168, PD_GAS: 96 }, { id: "2", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-02", PD_PETROLEO: 364.21, PD_GAS: 96 }, { id: "3", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-03", PD_PETROLEO: 361.559, PD_GAS: 96 }, { id: "4", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-04", PD_PETROLEO: 322.346, PD_GAS: 81 }, { id: "5", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-05", PD_PETROLEO: 339.027, PD_GAS: 44 }, { id: "6", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-06", PD_PETROLEO: 346.939, PD_GAS: 44 }, { id: "7", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-08", PD_PETROLEO: 321.934, PD_GAS: 44 }, { id: "8", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-09", PD_PETROLEO: 351.404, PD_GAS: 44 }, { id: "9", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 360.167, PD_GAS: 44 }, { id: "10", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 371.895, PD_GAS: 44 }, { id: "11", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-11", PD_PETROLEO: 356.232, PD_GAS: 66 }],
    keys = ['FECHA', 'PD_PETROLEO', 'PD_GAS'],
    result = data.reduce((r, o) => {
        keys.forEach((k, i) => (r[i] = r[i] || []).push(o[k]));
        return r;
    }, []);

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

How to group an array of objects through a key using Array reduce , How to groupBy using reduce in Javascript. For the person array, lets group the objects using the color value. In our object, there are two blue� 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 can use array of keys that you want vaues against and Array.prototype.map() to achieve this

const data = [{id: "1", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-01", PD_PETROLEO: 369.168, PD_GAS: 96},
       {id: "2", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-02", PD_PETROLEO: 364.21, PD_GAS: 96},
       {id: "3", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-03", PD_PETROLEO: 361.559, PD_GAS: 96},
       {id: "4", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-04", PD_PETROLEO: 322.346, PD_GAS: 81},
       {id: "5", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-05", PD_PETROLEO: 339.027, PD_GAS: 44},
       {id: "6", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-06", PD_PETROLEO: 346.939, PD_GAS: 44},
       {id: "7", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-08", PD_PETROLEO: 321.934, PD_GAS: 44},
       {id: "8", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-09", PD_PETROLEO: 351.404, PD_GAS: 44},
       {id: "9", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 360.167, PD_GAS: 44},
       {id: "10", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-10", PD_PETROLEO: 371.895, PD_GAS: 44},
      {id: "11", COMPLETAMIENTO: "CYB-003TS", FECHA: "2010-06-11", PD_PETROLEO: 356.232, PD_GAS: 66}];
      
 
 const output = data.map(obj => Object.values(obj));

 // Or you can use your required keys to get respective values
 const output2 = ['FECHA', 'PD_GAS', 'PD_AGUA', 'PD_PETROLEO'].map(key => data.map(obj => obj[key]));
 
 console.log(output2) // `PD_AGUA is in not in input so will show undefined, since question is not showing all data keys

A vanilla JS equivalent of lodash's groupBy() method, The groupBy() method takes a collection of items as an array and groups them together in an object based on some criteria. Let's say you two arrays. var nums = [6.1, 4.2, 6.3]; var words = ['one', 'two', 'three']; If you wanted to group all of the items in num together based on their integer value, you would do this. Basically this uses the reduce function to pass in an accumulator and current item, which then uses this to build your "grouped" arrays based on the passed in key. the inner part of the reduce may look complicated but essentially it is testing to see if the key of the passed in object exists and if it doesn't then create an empty array and append the current item to that newly created array otherwise using the spread operator pass in all the objects of the current key array and append

You can do something like this.

var array = [
      {
        id: 1,
        name: 'foo'
      },
      {
        id: 2,
        name: 'bar'
      },
    ];
    var columns = ['id','name'];
    var items = [];
    var newArray = [];
    columns.forEach((col,iter) => {
      items = array.map((v,k) => { return v[col];}); 
      newArray.push(items);  
    });
    console.log(newArray);

Group Array of JavaScript Objects by Key or Property Value � GitHub, Group Array of JavaScript Objects by Key or Property Value. Implementation. const groupBy = key => array => array.reduce((objectsByKeyValue, obj) => { const� array_count_values for javascript instead. Let's say I have simple JavaScript array like the following: var array = ['Car', 'Car', 'Truck', 'Boat', 'Truck']; I want to group and count of each so I would expect a key/value map of: { Car : 2, Truck : 2, Boat : 1 }

Most efficient method to groupby on an array of objects in Javascript, The most efficient method to group by a key on an array of objects in js is to on each element of the array, resulting in a single output value. Arrays of objects don't stay the same all the time. We almost always need to manipulate them. So let's take a look at how we can add objects to an already existing array. Add a new object at the start - Array.unshift. To add an object at the first position, use Array.unshift.

javascript group by property array of objects Code Example, Javascript queries related to “javascript group by property array of objects” js group objects in array on field � group array by value into a new� let fruits = new Array(2); console.log(fruits.length); // 2 console.log(fruits[0]); // undefined Array constructor with multiple parameters If more than one argument is passed to the constructor, a new Array with the given elements is created.

consolelog.io => groupBy() in Javascript, groupBy() method that groups an array by each object's property value. to group an array of objects by a common key value, maybe an array of events. be able to call our new groupBy() on any array object like <Array>. Arrays are Objects. Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access its "elements". In this example, person[0] returns John:

Comments
  • Thanks for your response, with the destructuring it was exactly what I was looking for.
  • @JimmyMontes yeah, but you already accepted another answer without destructing, at least you can upvote ;)