Create 2 Dimensional Array from Object with same key

Pat Doyle :

I have an array of objects that I want to transform. My dataset looks like this:

[
 {
  day: sunday,
  val: 20
 }, 
{
  day: sunday,
  val: 20
 }, 
{
  day: monday,
  val: 10
 },  
{
  day: monday,
  val: 30
 }, 
{
  day: tuesday,
  val: 5
 }, 
{
  day: tuesday,
  val: 5
 }
]

I am trying to transform the data to look like this:

Output: [[20,20], [10,30], [5, 5]]

Where each of the nested arrays are based on the Day of Week in object. Any ideas?

Thanks!

Mr. Polywhirl :

You could group your items by their day. After you have the groups, you can grab the values of the map and map the item lists to a list of val.

const data = [
  { day: 'sunday'  , val: 20 }, { day: 'sunday'  , val: 20 },
  { day: 'monday'  , val: 10 }, { day: 'monday'  , val: 30 },
  { day: 'tuesday' , val:  5 }, { day: 'tuesday' , val:  5 }
];

const transformed = Object.values(data.reduce((map, item) =>
  ({ ...map, [item.day] : [ ...(map[item.day] || []), item]
}), {})).map(list => list.map(item => item.val));

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

Alternatively, you can reduce the values right away, but you lose all the item properties.

const data = [
  { day: 'sunday'  , val: 20 }, { day: 'sunday'  , val: 20 },
  { day: 'monday'  , val: 10 }, { day: 'monday'  , val: 30 },
  { day: 'tuesday' , val:  5 }, { day: 'tuesday' , val:  5 }
];

const transformed = Object.values(data.reduce((map, item) =>
  ({ ...map, [item.day] : [ ...(map[item.day] || []), item.val] }), {}));

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

Here is a functional version:

const data = [
  { day: 'sunday'  , val: 20 }, { day: 'sunday'  , val: 20 },
  { day: 'monday'  , val: 10 }, { day: 'monday'  , val: 30 },
  { day: 'tuesday' , val:  5 }, { day: 'tuesday' , val:  5 }
];

const toMatrix = (list, key, valFn) => Object.values(data.reduce((map, item) =>
  ({ ...map, [item[key]] : [ ...(map[item[key]] || []), valFn(item) ] }), {}))

console.log(toMatrix(data, 'day', item => item.val));
.as-console-wrapper { top: 0; max-height: 100% !important; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Create an object from an array with same key, value values

Create 3-dimensional array from 2 dimensional array in matlab

Find the highest number with same key from a multi dimensional array

how do I create an array of objects with array in the object from an object with the same key in javascript

Create object with new key and assign same array

Create JSON object from Array and Multi-Dimensional Array

PHP - Combine 2 elements from the same 2 dimensional array

How to create an object from one dimensional array and static string

Re-create an array from 2 dimensional array

create an array from same key with different values

Compare 2 array key and values in the same object

Create a union type from an object key in an array

Create an array of values from object based on key

Create object from two array with custom key

Trying to create a 2 dimensional array from two existing arrays

LUA: How to Create 2-dimensional array/table from string

VBA Excel - Create pie chart from 2 dimensional array

Convert 2 Dimensional Array into Object

Remove object from array of objects (array1) if array2 does not have the same object key value

how to create a multi dimensional object from object?

Create 2 dimensional array with 2 one dimensional array

Create 2 dimensional array with 2 one dimensional array

Create new Array under same key from multiple Array in PHP

Create an array of objects with specific key, then remove that key from object

create an object array from another object accessing the key

Remove key from a two dimensional array (recordset)

Creating 2 dimensional array from larger 2 dimensional array

Create 2d array from 2 single dimensional array in Python

Dynamically create 2 dimensional object Javascript